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
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
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,42 +204,58 @@ class OpenAI(BaseBackend):
...
@@ -199,42 +204,58 @@ 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
):
try
:
for
attempt
in
range
(
retries
):
if
is_chat
:
try
:
if
kwargs
[
"stop"
]
is
None
:
if
is_chat
:
kwargs
.
pop
(
"stop"
)
if
"stop"
in
kwargs
and
kwargs
[
"stop"
]
is
None
:
ret
=
client
.
chat
.
completions
.
create
(
messages
=
prompt
,
**
kwargs
)
kwargs
.
pop
(
"stop"
)
comp
=
ret
.
choices
[
0
].
message
.
content
ret
=
client
.
chat
.
completions
.
create
(
messages
=
prompt
,
**
kwargs
)
else
:
comp
=
ret
.
choices
[
0
].
message
.
content
ret
=
client
.
completions
.
create
(
prompt
=
prompt
,
**
kwargs
)
if
isinstance
(
prompt
,
(
list
,
tuple
)):
comp
=
[
c
.
text
for
c
in
ret
.
choices
]
else
:
else
:
comp
=
ret
.
choices
[
0
].
text
ret
=
client
.
completions
.
create
(
prompt
=
prompt
,
**
kwargs
)
except
openai
.
OpenAIError
as
e
:
if
isinstance
(
prompt
,
(
list
,
tuple
)):
print
(
f
"OpenAI Error:
{
e
}
"
)
comp
=
[
c
.
text
for
c
in
ret
.
choices
]
raise
e
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
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
):
try
:
for
attempt
in
range
(
retries
):
if
is_chat
:
try
:
if
kwargs
[
"stop"
]
is
None
:
if
is_chat
:
kwargs
.
pop
(
"stop"
)
if
"stop"
in
kwargs
and
kwargs
[
"stop"
]
is
None
:
generator
=
client
.
chat
.
completions
.
create
(
kwargs
.
pop
(
"stop"
)
messages
=
prompt
,
stream
=
True
,
**
kwargs
generator
=
client
.
chat
.
completions
.
create
(
)
messages
=
prompt
,
stream
=
True
,
**
kwargs
for
ret
in
generator
:
)
content
=
ret
.
choices
[
0
].
delta
.
content
for
ret
in
generator
:
yield
content
or
""
,
{}
content
=
ret
.
choices
[
0
].
delta
.
content
else
:
yield
content
or
""
,
{}
generator
=
client
.
completions
.
create
(
prompt
=
prompt
,
stream
=
True
,
**
kwargs
)
else
:
for
ret
in
generator
:
generator
=
client
.
completions
.
create
(
content
=
ret
.
choices
[
0
].
text
prompt
=
prompt
,
stream
=
True
,
**
kwargs
yield
content
or
""
,
{}
)
except
openai
.
OpenAIError
as
e
:
for
ret
in
generator
:
print
(
f
"OpenAI Error:
{
e
}
"
)
content
=
ret
.
choices
[
0
].
text
raise
e
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