From 5d49af0473cda175fee24c26489c5014eabf5941 Mon Sep 17 00:00:00 2001 From: Masahiro Ogawa Date: Fri, 28 Jun 2024 11:17:41 +0900 Subject: [PATCH 1/2] add config for loading openai model. --- README.md | 2 +- api/config.json | 3 +++ api/requirements.txt | 3 ++- api/src/llm/openai.py | 2 +- api/src/main.py | 20 ++++++++++++----- ui/src/chat-with-kg/App.tsx | 44 ++++++++++++++++++------------------- 6 files changed, 44 insertions(+), 30 deletions(-) create mode 100644 api/config.json diff --git a/README.md b/README.md index 54bdc7e..23c4eee 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ To simplify the process of running the demos, we have incorporated scripts that 1. Navigate into the root directory. 2. Create an env file. You can use the env.example file as a template. (The open API key is optional and can be provided from the UI instead) -3. run `docker-compose up` to build the images. +3. run `docker compose up` to build the images. This will start the backend and frontend servers, and you can access the demos at the following URLs: diff --git a/api/config.json b/api/config.json new file mode 100644 index 0000000..8be7249 --- /dev/null +++ b/api/config.json @@ -0,0 +1,3 @@ +{ + "model_name": "gpt-4o" +} \ No newline at end of file diff --git a/api/requirements.txt b/api/requirements.txt index a390d8d..2f98db9 100644 --- a/api/requirements.txt +++ b/api/requirements.txt @@ -7,4 +7,5 @@ retry==0.9.2 tiktoken==0.4.0 python-dotenv==1.0.0 websockets===11.0.3 -gunicorn===20.1.0 \ No newline at end of file +gunicorn===20.1.0 +jsonschema==3.0 \ No newline at end of file diff --git a/api/src/llm/openai.py b/api/src/llm/openai.py index b09e311..e51a5d5 100644 --- a/api/src/llm/openai.py +++ b/api/src/llm/openai.py @@ -15,7 +15,7 @@ class OpenAIChat(BaseLLM): def __init__( self, openai_api_key: str, - model_name: str = "gpt-3.5-turbo", + model_name: str = "gpt-4o", max_tokens: int = 1000, temperature: float = 0.0, ) -> None: diff --git a/api/src/main.py b/api/src/main.py index cbb35b2..2836140 100644 --- a/api/src/main.py +++ b/api/src/main.py @@ -19,6 +19,7 @@ from fewshot_examples import get_fewshot_examples from llm.openai import OpenAIChat from pydantic import BaseModel +import json class Payload(BaseModel): @@ -37,6 +38,11 @@ class questionProposalPayload(BaseModel): api_key: Optional[str] +def load_config(filename): + with open(filename, "r") as f: + return json.load(f) + + # Maximum number of records used in the context HARD_LIMIT_CONTEXT_RECORDS = 10 @@ -76,12 +82,14 @@ async def questionProposalsForCurrentDb(payload: questionProposalPayload): detail="Please set OPENAI_API_KEY environment variable or send it as api_key in the request body", ) api_key = openai_api_key if openai_api_key else payload.api_key + + config = load_config("../config.json") questionProposalGenerator = QuestionProposalGenerator( database=neo4j_connection, llm=OpenAIChat( openai_api_key=api_key, - model_name="gpt-3.5-turbo-0613", + model_name=config.get("model_name", "gpt-4o"), max_tokens=512, temperature=0.8, ), @@ -130,12 +138,12 @@ async def onToken(token): default_llm = OpenAIChat( openai_api_key=api_key, - model_name=data.get("model_name", "gpt-3.5-turbo-0613"), + model_name=data.get("model_name", "gpt-4o"), ) summarize_results = SummarizeCypherResult( llm=OpenAIChat( openai_api_key=api_key, - model_name="gpt-3.5-turbo-0613", + model_name=data.get("model_name", "gpt-4o"), max_tokens=128, ) ) @@ -205,8 +213,9 @@ async def root(payload: ImportPayload): try: result = "" + config = load_config("../config.json") llm = OpenAIChat( - openai_api_key=api_key, model_name="gpt-3.5-turbo-16k", max_tokens=4000 + openai_api_key=api_key, model_name=config.get("model_name","gpt-4o"), max_tokens=4000 ) if not payload.neo4j_schema: @@ -245,10 +254,11 @@ async def companyInformation(payload: companyReportPayload): detail="Please set OPENAI_API_KEY environment variable or send it as api_key in the request body", ) api_key = openai_api_key if openai_api_key else payload.api_key + config = load_config("../config.json") llm = OpenAIChat( openai_api_key=api_key, - model_name="gpt-3.5-turbo-16k-0613", + model_name=config.get("model_name", "gpt-4o"), max_tokens=512, ) print("Running company report for " + payload.company) diff --git a/ui/src/chat-with-kg/App.tsx b/ui/src/chat-with-kg/App.tsx index 12a39c4..0cb98fa 100644 --- a/ui/src/chat-with-kg/App.tsx +++ b/ui/src/chat-with-kg/App.tsx @@ -15,23 +15,23 @@ const SEND_REQUESTS = true; const chatMessageObjects: ChatMessageObject[] = SEND_REQUESTS ? [] : [ - { - id: 0, - type: "input", - sender: "self", - message: - "This is the first message which has decently long text and would denote something typed by the user", - complete: true, - }, - { - id: 1, - type: "text", - sender: "bot", - message: - "And here is another message which would denote a response from the server, which for now will only be text", - complete: true, - }, - ]; + { + id: 0, + type: "input", + sender: "self", + message: + "This is the first message which has decently long text and would denote something typed by the user", + complete: true, + }, + { + id: 1, + type: "text", + sender: "bot", + message: + "And here is another message which would denote a response from the server, which for now will only be text", + complete: true, + }, + ]; const URI = import.meta.env.VITE_KG_CHAT_BACKEND_ENDPOINT ?? @@ -73,7 +73,7 @@ function App() { const [modalIsOpen, setModalIsOpen] = useState(false); const [apiKey, setApiKey] = useState(loadKeyFromStorage() || ""); const [sampleQuestions, setSampleQuestions] = useState([]); - const [text2cypherModel, setText2cypherModel] = useState("gpt-3.5-turbo-0613"); + const [text2cypherModel, setText2cypherModel] = useState("gpt-4o"); const showContent = serverAvailable && !needsApiKeyLoading; @@ -268,12 +268,12 @@ function App() { )} -
+
-
+
{!serverAvailable && (
Server is unavailable, please reload the page to try again.
From ad82149b98331ec42f1c530a999f3842b83c9229 Mon Sep 17 00:00:00 2001 From: Masahiro Ogawa Date: Fri, 28 Jun 2024 11:49:41 +0900 Subject: [PATCH 2/2] fix wrong path in readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 23c4eee..a32011a 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ The news articles are linked to the mentioned entity, while the actual text is s To run the project on your own database, follow these two steps: 1. Set appropriate database credentials in `.env` file -2. Remove or set appropriate Cypher examples in `api/fewshot_examples.py` file +2. Remove or set appropriate Cypher examples in `api/src/fewshot_examples.py` file ## Contributing