Skip to content

Commit e720faf

Browse files
Bump openai version to >=1 and fix up APIs where needed
1 parent bbcc266 commit e720faf

File tree

4 files changed

+37
-28
lines changed

4 files changed

+37
-28
lines changed

Chatbot.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import openai
1+
from openai import OpenAI
22
import streamlit as st
33

44
with st.sidebar:
@@ -20,10 +20,10 @@
2020
st.info("Please add your OpenAI API key to continue.")
2121
st.stop()
2222

23-
openai.api_key = openai_api_key
23+
client = OpenAI(api_key=openai_api_key)
2424
st.session_state.messages.append({"role": "user", "content": prompt})
2525
st.chat_message("user").write(prompt)
26-
response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=st.session_state.messages)
27-
msg = response.choices[0].message
28-
st.session_state.messages.append(msg)
29-
st.chat_message("assistant").write(msg.content)
26+
response = client.chat.completions.create(model="gpt-3.5-turbo", messages=st.session_state.messages)
27+
msg = response.choices[0].message.content
28+
st.session_state.messages.append({"role": "assistant", "content": msg})
29+
st.chat_message("assistant").write(msg)

app_test.py

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,39 @@
1+
import datetime
12
from unittest.mock import patch
23
from streamlit.testing.v1 import AppTest
3-
from openai.openai_object import OpenAIObject
4-
5-
6-
# See https://github.com/openai/openai-python/issues/398
7-
def create_openai_object_sync(response: str, role: str = "assistant") -> OpenAIObject:
8-
obj = OpenAIObject()
9-
message = OpenAIObject()
10-
content = OpenAIObject()
11-
content.content = response
12-
content.role = role
13-
message.message = content
14-
obj.choices = [message]
15-
return obj
16-
17-
18-
@patch("openai.ChatCompletion.create")
4+
from openai.types.chat import ChatCompletionMessage
5+
from openai.types.chat.chat_completion import ChatCompletion, Choice
6+
7+
8+
# See https://github.com/openai/openai-python/issues/715#issuecomment-1809203346
9+
def create_chat_completion(response: str, role: str = "assistant") -> ChatCompletion:
10+
return ChatCompletion(
11+
id="foo",
12+
model="gpt-3.5-turbo",
13+
object="chat.completion",
14+
choices=[
15+
Choice(
16+
finish_reason="stop",
17+
index=0,
18+
message=ChatCompletionMessage(
19+
content=response,
20+
role=role,
21+
),
22+
)
23+
],
24+
created=int(datetime.datetime.now().timestamp()),
25+
)
26+
27+
28+
@patch("openai.resources.chat.Completions.create")
1929
def test_Chatbot(openai_create):
2030
at = AppTest.from_file("Chatbot.py").run()
2131
assert not at.exception
2232
at.chat_input[0].set_value("Do you know any jokes?").run()
2333
assert at.info[0].value == "Please add your OpenAI API key to continue."
2434

2535
JOKE = "Why did the chicken cross the road? To get to the other side."
26-
openai_create.return_value = create_openai_object_sync(JOKE)
36+
openai_create.return_value = create_chat_completion(JOKE)
2737
at.text_input(key="chatbot_api_key").set_value("sk-...")
2838
at.chat_input[0].set_value("Do you know any jokes?").run()
2939
print(at)

pages/5_Chat_with_user_feedback.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import openai
1+
from openai import OpenAI
22
import streamlit as st
33
from streamlit_feedback import streamlit_feedback
44
import trubrics
@@ -34,9 +34,8 @@
3434
if not openai_api_key:
3535
st.info("Please add your OpenAI API key to continue.")
3636
st.stop()
37-
else:
38-
openai.api_key = openai_api_key
39-
response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages)
37+
client = OpenAI(api_key=openai_api_key)
38+
response = client.chat.completions.create(model="gpt-3.5-turbo", messages=messages)
4039
st.session_state["response"] = response.choices[0].message.content
4140
with st.chat_message("assistant"):
4241
messages.append({"role": "assistant", "content": st.session_state["response"]})

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
streamlit>=1.28
22
langchain>=0.0.217
3-
openai
3+
openai>=1.2
44
duckduckgo-search
55
anthropic>=0.3.0
66
trubrics>=1.4.3

0 commit comments

Comments
 (0)