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
zhaoyu6
sglang
Commits
e2bf732b
"tests/vscode:/vscode.git/clone" did not exist on "bf4e71c20ef3e46ea37035a038c311821fce9d6b"
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
Hide 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
import
numpy
as
np
...
...
@@ -13,6 +15,9 @@ except ImportError as e:
openai
=
tiktoken
=
e
logger
=
logging
.
getLogger
(
"openai"
)
def
create_logit_bias_int
(
tokenizer
):
"""Get logit bias for integer numbers."""
int_token_ids
=
[]
...
...
@@ -199,42 +204,58 @@ class OpenAI(BaseBackend):
return
decision
,
scores
,
scores
def
openai_completion
(
client
,
is_chat
=
None
,
prompt
=
None
,
**
kwargs
):
try
:
if
is_chat
:
if
kwargs
[
"stop"
]
is
None
:
kwargs
.
pop
(
"stop"
)
ret
=
client
.
chat
.
completions
.
create
(
messages
=
prompt
,
**
kwargs
)
comp
=
ret
.
choices
[
0
].
message
.
content
else
:
ret
=
client
.
completions
.
create
(
prompt
=
prompt
,
**
kwargs
)
if
isinstance
(
prompt
,
(
list
,
tuple
)):
comp
=
[
c
.
text
for
c
in
ret
.
choices
]
def
openai_completion
(
client
,
retries
=
3
,
is_chat
=
None
,
prompt
=
None
,
**
kwargs
):
for
attempt
in
range
(
retries
):
try
:
if
is_chat
:
if
"stop"
in
kwargs
and
kwargs
[
"stop"
]
is
None
:
kwargs
.
pop
(
"stop"
)
ret
=
client
.
chat
.
completions
.
create
(
messages
=
prompt
,
**
kwargs
)
comp
=
ret
.
choices
[
0
].
message
.
content
else
:
comp
=
ret
.
choices
[
0
].
text
except
openai
.
OpenAIError
as
e
:
print
(
f
"OpenAI Error:
{
e
}
"
)
raise
e
ret
=
client
.
completions
.
create
(
prompt
=
prompt
,
**
kwargs
)
if
isinstance
(
prompt
,
(
list
,
tuple
)):
comp
=
[
c
.
text
for
c
in
ret
.
choices
]
else
:
comp
=
ret
.
choices
[
0
].
text
break
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
return
comp
def
openai_completion_stream
(
client
,
is_chat
=
None
,
prompt
=
None
,
**
kwargs
):
try
:
if
is_chat
:
if
kwargs
[
"stop"
]
is
None
:
kwargs
.
pop
(
"stop"
)
generator
=
client
.
chat
.
completions
.
create
(
messages
=
prompt
,
stream
=
True
,
**
kwargs
)
for
ret
in
generator
:
content
=
ret
.
choices
[
0
].
delta
.
content
yield
content
or
""
,
{}
else
:
generator
=
client
.
completions
.
create
(
prompt
=
prompt
,
stream
=
True
,
**
kwargs
)
for
ret
in
generator
:
content
=
ret
.
choices
[
0
].
text
yield
content
or
""
,
{}
except
openai
.
OpenAIError
as
e
:
print
(
f
"OpenAI Error:
{
e
}
"
)
raise
e
def
openai_completion_stream
(
client
,
retries
=
3
,
is_chat
=
None
,
prompt
=
None
,
**
kwargs
):
for
attempt
in
range
(
retries
):
try
:
if
is_chat
:
if
"stop"
in
kwargs
and
kwargs
[
"stop"
]
is
None
:
kwargs
.
pop
(
"stop"
)
generator
=
client
.
chat
.
completions
.
create
(
messages
=
prompt
,
stream
=
True
,
**
kwargs
)
for
ret
in
generator
:
content
=
ret
.
choices
[
0
].
delta
.
content
yield
content
or
""
,
{}
else
:
generator
=
client
.
completions
.
create
(
prompt
=
prompt
,
stream
=
True
,
**
kwargs
)
for
ret
in
generator
:
content
=
ret
.
choices
[
0
].
text
yield
content
or
""
,
{}
break
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
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