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

Extract openai model form #16

Merged
merged 1 commit into from
Mar 24, 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
20 changes: 20 additions & 0 deletions doccano_mini/components.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import os

import streamlit as st
from langchain.llms import OpenAI
from langchain.schema import BaseLanguageModel

CODE = """from langchain.chains import load_chain

Expand All @@ -19,3 +23,19 @@ def display_download_button():
def display_usage():
st.header("Usage")
st.code(CODE)


def openai_model_form() -> BaseLanguageModel:
# https://platform.openai.com/docs/models/gpt-3-5
AVAILABLE_MODELS = (
"gpt-3.5-turbo",
"gpt-3.5-turbo-0301",
"text-davinci-003",
"text-davinci-002",
"code-davinci-002",
)
api_key = st.text_input("API key", value=os.environ.get("OPENAI_API_KEY", ""), type="password")
model_name = st.selectbox("Model", AVAILABLE_MODELS, index=2)
temperature = st.slider("Temperature", min_value=0.0, max_value=1.0, value=0.7, step=0.01)
top_p = st.slider("Top-p", min_value=0.0, max_value=1.0, value=1.0, step=0.01)
return OpenAI(model_name=model_name, temperature=temperature, top_p=top_p, openai_api_key=api_key) # type:ignore
8 changes: 0 additions & 8 deletions doccano_mini/models.py

This file was deleted.

18 changes: 7 additions & 11 deletions doccano_mini/pages/01_Text_Classification.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import os

import streamlit as st
from langchain.chains import LLMChain
from langchain.llms import OpenAI

from doccano_mini.components import display_download_button, display_usage
from doccano_mini.components import (
display_download_button,
display_usage,
openai_model_form,
)
from doccano_mini.examples import make_classification_example
from doccano_mini.models import AVAILABLE_MODELS
from doccano_mini.prompts import make_classification_prompt

st.title("Text Classification")
Expand All @@ -24,20 +24,16 @@
prompt.prefix = instruction

st.header("Test")
api_key = st.text_input("Enter API key", value=os.environ.get("OPENAI_API_KEY", ""), type="password")
col1, col2 = st.columns([3, 1])
text = col1.text_area(label="Please enter your text.", value="", height=300)

# Use text-davinci-003 by default.
model_name = col2.selectbox("Model", AVAILABLE_MODELS, index=2)
temperature = col2.slider("Temperature", min_value=0.0, max_value=1.0, value=0.7, step=0.01)
top_p = col2.slider("Top-p", min_value=0.0, max_value=1.0, value=1.0, step=0.01)
with col2:
llm = openai_model_form()

with st.expander("See your prompt"):
st.markdown(f"```\n{prompt.format(input=text)}\n```")

if st.button("Predict"):
llm = OpenAI(model_name=model_name, temperature=temperature, top_p=top_p, openai_api_key=api_key) # type:ignore
chain = LLMChain(llm=llm, prompt=prompt)
response = chain.run(text)
label = response.split(":")[1]
Expand Down
17 changes: 6 additions & 11 deletions doccano_mini/pages/09_Task_Free.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import os

import streamlit as st
from langchain.chains import LLMChain
from langchain.llms import OpenAI

from doccano_mini.components import display_download_button, display_usage
from doccano_mini.components import (
display_download_button,
display_usage,
openai_model_form,
)
from doccano_mini.examples import make_task_free_example
from doccano_mini.models import AVAILABLE_MODELS
from doccano_mini.prompts import make_task_free_prompt

st.title("Task Free")
Expand All @@ -31,13 +31,8 @@

st.markdown(f"Your prompt\n```\n{prompt.format(**inputs)}\n```")

# Use text-davinci-003 by default.
api_key = st.text_input("Enter API key", value=os.environ.get("OPENAI_API_KEY", ""), type="password")
model_name = st.selectbox("Model", AVAILABLE_MODELS, index=2)
temperature = st.slider("Temperature", min_value=0.0, max_value=1.0, value=0.7, step=0.01)
top_p = st.slider("Top-p", min_value=0.0, max_value=1.0, value=1.0, step=0.01)
llm = openai_model_form()
if st.button("Predict"):
llm = OpenAI(model_name=model_name, temperature=temperature, top_p=top_p, openai_api_key=api_key) # type:ignore
chain = LLMChain(llm=llm, prompt=prompt)
response = chain.run(**inputs)
st.text(response)
Expand Down