Skip to content

Commit

Permalink
update openai api
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffkayne committed Nov 15, 2023
1 parent 0ba20b2 commit c8c30b0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
17 changes: 8 additions & 9 deletions examples/streamlit/llm_app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import openai
import streamlit as st
from trubrics_utils import trubrics_config, trubrics_successful_feedback
from openai import OpenAI
from trubrics_utils import trubrics_config

from trubrics.integrations.streamlit import FeedbackCollector

Expand Down Expand Up @@ -36,17 +36,19 @@
help="Consult https://platform.openai.com/docs/models/gpt-3-5 for model info.",
)

openai.api_key = st.secrets.get("OPENAI_API_KEY")
if openai.api_key is None:
openai_api_key = st.secrets.get("OPENAI_API_KEY")
if openai_api_key is None:
st.info("Please add your OpenAI API key to continue.")
st.stop()

client = OpenAI(api_key=openai_api_key)

prompt = st.text_area(label="Prompt", label_visibility="collapsed", placeholder="What would you like to know?")
button = st.button(f"Ask {model}")

if button:
response = openai.ChatCompletion.create(model=model, messages=[{"role": "user", "content": prompt}])
response_text = response.choices[0].message["content"]
response = client.chat.completions.create(model=model, messages=[{"role": "user", "content": prompt}])
response_text = response.choices[0].message.content
st.session_state.logged_prompt = collector.log_prompt(
config_model={"model": model}, prompt=prompt, generation=response_text, tags=["llm_app.py"], user_id=email
)
Expand All @@ -67,6 +69,3 @@
key=f"feedback_{st.session_state.feedback_key}", # overwrite with new key
user_id=email,
)

if feedback:
trubrics_successful_feedback(feedback)
12 changes: 6 additions & 6 deletions examples/streamlit/llm_chatbot.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import uuid

import openai
import streamlit as st
from openai import OpenAI
from trubrics_utils import trubrics_config

from trubrics.integrations.streamlit import FeedbackCollector
Expand Down Expand Up @@ -78,18 +78,18 @@ def init_trubrics(email, password):
st.info("Please add your OpenAI API key to continue.")
st.stop()
else:
openai.api_key = openai_api_key
client = OpenAI(api_key=openai_api_key)

with st.chat_message("assistant"):
if stream:
message_placeholder = st.empty()
generation = ""
for response in openai.ChatCompletion.create(model=model, messages=messages, stream=True):
generation += response.choices[0].delta.get("content", "")
for part in client.chat.completions.create(model=model, messages=messages, stream=True):
generation += part.choices[0].delta.content or ""
message_placeholder.markdown(generation + "▌")
message_placeholder.markdown(generation)
else:
response = openai.ChatCompletion.create(model=model, messages=messages)
response = client.chat.completions.create(model=model, messages=messages)
generation = response.choices[0].message.content
st.write(generation)

Expand All @@ -103,4 +103,4 @@ def init_trubrics(email, password):
)
st.session_state.prompt_ids.append(logged_prompt.id)
messages.append({"role": "assistant", "content": generation})
st.experimental_rerun() # force rerun of app, to load last feedback component
st.rerun() # force rerun of app, to load last feedback component

0 comments on commit c8c30b0

Please sign in to comment.