Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
change
sglang
Commits
e2bf732b
Unverified
Commit
e2bf732b
authored
Feb 06, 2024
by
LiviaSun
Committed by
GitHub
Feb 05, 2024
Browse files
add openai error handler with retry and logger (#148)
parent
322421fa
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
55 additions
and
34 deletions
+55
-34
python/sglang/backend/openai.py
python/sglang/backend/openai.py
+55
-34
No files found.
python/sglang/backend/openai.py
View file @
e2bf732b
import
logging
import
time
from
typing
import
Callable
,
List
,
Optional
,
Union
from
typing
import
Callable
,
List
,
Optional
,
Union
import
numpy
as
np
import
numpy
as
np
...
@@ -13,6 +15,9 @@ except ImportError as e:
...
@@ -13,6 +15,9 @@ except ImportError as e:
openai
=
tiktoken
=
e
openai
=
tiktoken
=
e
logger
=
logging
.
getLogger
(
"openai"
)
def
create_logit_bias_int
(
tokenizer
):
def
create_logit_bias_int
(
tokenizer
):
"""Get logit bias for integer numbers."""
"""Get logit bias for integer numbers."""
int_token_ids
=
[]
int_token_ids
=
[]
...
@@ -199,10 +204,11 @@ class OpenAI(BaseBackend):
...
@@ -199,10 +204,11 @@ class OpenAI(BaseBackend):
return
decision
,
scores
,
scores
return
decision
,
scores
,
scores
def
openai_completion
(
client
,
is_chat
=
None
,
prompt
=
None
,
**
kwargs
):
def
openai_completion
(
client
,
retries
=
3
,
is_chat
=
None
,
prompt
=
None
,
**
kwargs
):
for
attempt
in
range
(
retries
):
try
:
try
:
if
is_chat
:
if
is_chat
:
if
kwargs
[
"stop"
]
is
None
:
if
"stop"
in
kwargs
and
kwargs
[
"stop"
]
is
None
:
kwargs
.
pop
(
"stop"
)
kwargs
.
pop
(
"stop"
)
ret
=
client
.
chat
.
completions
.
create
(
messages
=
prompt
,
**
kwargs
)
ret
=
client
.
chat
.
completions
.
create
(
messages
=
prompt
,
**
kwargs
)
comp
=
ret
.
choices
[
0
].
message
.
content
comp
=
ret
.
choices
[
0
].
message
.
content
...
@@ -212,17 +218,24 @@ def openai_completion(client, is_chat=None, prompt=None, **kwargs):
...
@@ -212,17 +218,24 @@ def openai_completion(client, is_chat=None, prompt=None, **kwargs):
comp
=
[
c
.
text
for
c
in
ret
.
choices
]
comp
=
[
c
.
text
for
c
in
ret
.
choices
]
else
:
else
:
comp
=
ret
.
choices
[
0
].
text
comp
=
ret
.
choices
[
0
].
text
except
openai
.
OpenAIError
as
e
:
break
print
(
f
"OpenAI Error:
{
e
}
"
)
except
(
openai
.
APIError
,
openai
.
APIConnectionError
,
openai
.
RateLimitError
)
as
e
:
logger
.
error
(
f
"OpenAI Error:
{
e
}
. Waiting 5 seconds..."
)
time
.
sleep
(
5
)
if
attempt
==
retries
-
1
:
raise
e
except
Exception
as
e
:
logger
.
error
(
f
"RuntimeError
{
e
}
."
)
raise
e
raise
e
return
comp
return
comp
def
openai_completion_stream
(
client
,
is_chat
=
None
,
prompt
=
None
,
**
kwargs
):
def
openai_completion_stream
(
client
,
retries
=
3
,
is_chat
=
None
,
prompt
=
None
,
**
kwargs
):
for
attempt
in
range
(
retries
):
try
:
try
:
if
is_chat
:
if
is_chat
:
if
kwargs
[
"stop"
]
is
None
:
if
"stop"
in
kwargs
and
kwargs
[
"stop"
]
is
None
:
kwargs
.
pop
(
"stop"
)
kwargs
.
pop
(
"stop"
)
generator
=
client
.
chat
.
completions
.
create
(
generator
=
client
.
chat
.
completions
.
create
(
messages
=
prompt
,
stream
=
True
,
**
kwargs
messages
=
prompt
,
stream
=
True
,
**
kwargs
...
@@ -231,10 +244,18 @@ def openai_completion_stream(client, is_chat=None, prompt=None, **kwargs):
...
@@ -231,10 +244,18 @@ def openai_completion_stream(client, is_chat=None, prompt=None, **kwargs):
content
=
ret
.
choices
[
0
].
delta
.
content
content
=
ret
.
choices
[
0
].
delta
.
content
yield
content
or
""
,
{}
yield
content
or
""
,
{}
else
:
else
:
generator
=
client
.
completions
.
create
(
prompt
=
prompt
,
stream
=
True
,
**
kwargs
)
generator
=
client
.
completions
.
create
(
prompt
=
prompt
,
stream
=
True
,
**
kwargs
)
for
ret
in
generator
:
for
ret
in
generator
:
content
=
ret
.
choices
[
0
].
text
content
=
ret
.
choices
[
0
].
text
yield
content
or
""
,
{}
yield
content
or
""
,
{}
except
openai
.
OpenAIError
as
e
:
break
print
(
f
"OpenAI Error:
{
e
}
"
)
except
(
openai
.
APIError
,
openai
.
APIConnectionError
,
openai
.
RateLimitError
)
as
e
:
logger
.
error
(
f
"OpenAI Error:
{
e
}
. Waiting 5 seconds..."
)
time
.
sleep
(
5
)
if
attempt
==
retries
-
1
:
raise
e
except
Exception
as
e
:
logger
.
error
(
f
"RuntimeError
{
e
}
."
)
raise
e
raise
e
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment