Skip to content

Commit

Permalink
Merge pull request #29 from sfc-gh-jcarroll/update-openai-v1
Browse files Browse the repository at this point in the history
Bump openai version to >=1 and fix up APIs where needed
  • Loading branch information
sfc-gh-jcarroll authored Nov 14, 2023
2 parents bbcc266 + e720faf commit 54183c1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 28 deletions.
12 changes: 6 additions & 6 deletions Chatbot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import openai
from openai import OpenAI
import streamlit as st

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

openai.api_key = openai_api_key
client = OpenAI(api_key=openai_api_key)
st.session_state.messages.append({"role": "user", "content": prompt})
st.chat_message("user").write(prompt)
response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=st.session_state.messages)
msg = response.choices[0].message
st.session_state.messages.append(msg)
st.chat_message("assistant").write(msg.content)
response = client.chat.completions.create(model="gpt-3.5-turbo", messages=st.session_state.messages)
msg = response.choices[0].message.content
st.session_state.messages.append({"role": "assistant", "content": msg})
st.chat_message("assistant").write(msg)
44 changes: 27 additions & 17 deletions app_test.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@
import datetime
from unittest.mock import patch
from streamlit.testing.v1 import AppTest
from openai.openai_object import OpenAIObject


# See https://github.com/openai/openai-python/issues/398
def create_openai_object_sync(response: str, role: str = "assistant") -> OpenAIObject:
obj = OpenAIObject()
message = OpenAIObject()
content = OpenAIObject()
content.content = response
content.role = role
message.message = content
obj.choices = [message]
return obj


@patch("openai.ChatCompletion.create")
from openai.types.chat import ChatCompletionMessage
from openai.types.chat.chat_completion import ChatCompletion, Choice


# See https://github.com/openai/openai-python/issues/715#issuecomment-1809203346
def create_chat_completion(response: str, role: str = "assistant") -> ChatCompletion:
return ChatCompletion(
id="foo",
model="gpt-3.5-turbo",
object="chat.completion",
choices=[
Choice(
finish_reason="stop",
index=0,
message=ChatCompletionMessage(
content=response,
role=role,
),
)
],
created=int(datetime.datetime.now().timestamp()),
)


@patch("openai.resources.chat.Completions.create")
def test_Chatbot(openai_create):
at = AppTest.from_file("Chatbot.py").run()
assert not at.exception
at.chat_input[0].set_value("Do you know any jokes?").run()
assert at.info[0].value == "Please add your OpenAI API key to continue."

JOKE = "Why did the chicken cross the road? To get to the other side."
openai_create.return_value = create_openai_object_sync(JOKE)
openai_create.return_value = create_chat_completion(JOKE)
at.text_input(key="chatbot_api_key").set_value("sk-...")
at.chat_input[0].set_value("Do you know any jokes?").run()
print(at)
Expand Down
7 changes: 3 additions & 4 deletions pages/5_Chat_with_user_feedback.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import openai
from openai import OpenAI
import streamlit as st
from streamlit_feedback import streamlit_feedback
import trubrics
Expand Down Expand Up @@ -34,9 +34,8 @@
if not openai_api_key:
st.info("Please add your OpenAI API key to continue.")
st.stop()
else:
openai.api_key = openai_api_key
response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages)
client = OpenAI(api_key=openai_api_key)
response = client.chat.completions.create(model="gpt-3.5-turbo", messages=messages)
st.session_state["response"] = response.choices[0].message.content
with st.chat_message("assistant"):
messages.append({"role": "assistant", "content": st.session_state["response"]})
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
streamlit>=1.28
langchain>=0.0.217
openai
openai>=1.2
duckduckgo-search
anthropic>=0.3.0
trubrics>=1.4.3
Expand Down

0 comments on commit 54183c1

Please sign in to comment.