Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 19 additions & 26 deletions fastchat/serve/api_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,19 @@ def openai_api_stream_iter(
):
import openai

is_azure = False
api_key = api_key or os.environ["OPENAI_API_KEY"]

if "azure" in model_name:
is_azure = True
openai.api_type = "azure"
openai.api_version = "2023-07-01-preview"
client = openai.AzureOpenAI(
api_version="2023-07-01-preview",
azure_endpoint=api_base or "https://api.openai.com/v1",
api_key=api_key,
)
else:
openai.api_type = "open_ai"
openai.api_version = None
client = openai.OpenAI(
base_url=api_base or "https://api.openai.com/v1", api_key=api_key
)

openai.api_base = api_base or "https://api.openai.com/v1"
openai.api_key = api_key or os.environ["OPENAI_API_KEY"]
if model_name == "gpt-4-turbo":
model_name = "gpt-4-1106-preview"

Expand All @@ -123,26 +125,17 @@ def openai_api_stream_iter(
}
logger.info(f"==== request ====\n{gen_params}")

if is_azure:
res = openai.ChatCompletion.create(
engine=model_name,
messages=messages,
temperature=temperature,
max_tokens=max_new_tokens,
stream=True,
)
else:
res = openai.ChatCompletion.create(
model=model_name,
messages=messages,
temperature=temperature,
max_tokens=max_new_tokens,
stream=True,
)
res = client.chat.completions.create(
model=model_name,
messages=messages,
temperature=temperature,
max_tokens=max_new_tokens,
stream=True,
)
text = ""
for chunk in res:
if len(chunk["choices"]) > 0:
text += chunk["choices"][0]["delta"].get("content", "")
if len(chunk.choices) > 0:
text += chunk.choices[0].delta.content or ""
data = {
"text": text,
"error_code": 0,
Expand Down
1 change: 0 additions & 1 deletion fastchat/serve/gradio_block_arena_vision.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ def build_single_vision_language_model_ui(models, add_promotion_links=False):
notice_markdown = f"""
# 🏔️ Chat with Open Large Vision-Language Models
{promotion}
## 🤖 Choose any model to chat
"""

state = gr.State()
Expand Down
13 changes: 5 additions & 8 deletions fastchat/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,24 +154,21 @@ def oai_moderation(text):
"""
import openai

openai.api_base = "https://api.openai.com/v1"
openai.api_key = os.environ["OPENAI_API_KEY"]
openai.api_type = "open_ai"
openai.api_version = None
client = openai.OpenAI(api_key=os.environ["OPENAI_API_KEY"])

threshold_dict = {
"sexual": 0.2,
}
MAX_RETRY = 3
for _ in range(MAX_RETRY):
try:
res = openai.Moderation.create(input=text)
flagged = res["results"][0]["flagged"]
res = client.moderations.create(input=text)
flagged = res.results[0].flagged
for category, threshold in threshold_dict.items():
if res["results"][0]["category_scores"][category] > threshold:
if getattr(res.results[0].category_scores, category) > threshold:
flagged = True
break
except (openai.error.OpenAIError, KeyError, IndexError) as e:
except (openai.OpenAIError, KeyError, IndexError) as e:
# flag true to be conservative
flagged = True
print(f"MODERATION ERROR: {e}\nInput: {text}")
Expand Down