Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump openai version to >=1 and fix up APIs where needed #29

Merged
merged 1 commit into from
Nov 14, 2023
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
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