Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -11,15 +11,13 @@ from io import StringIO # for redirect_stdout
|
|
11 |
from functools import wraps # for caching
|
12 |
import contextlib # for redirect_stdout
|
13 |
import tldextract
|
14 |
-
from langchain.prompts import PromptTemplate
|
15 |
-
from langchain.chains import LLMChain
|
16 |
-
from langchain_openai import ChatOpenAI
|
17 |
import requests
|
18 |
import streamlit as st
|
19 |
import pandas as pd
|
20 |
import streamlit.components.v1 as components
|
21 |
import json
|
22 |
import os
|
|
|
23 |
|
24 |
|
25 |
# ---------------------------------------------------------------------------- #
|
@@ -87,8 +85,12 @@ else:
|
|
87 |
ADD AS LEARN MORE LINKS FOR THE FIRST TEXT BLOCK LINKS TO structured data https://wordlift.io/blog/en/entity/structured-data/ and schema.org https://wordlift.io/blog/en/entity/schema-org/ TO PROVIDE ADDITIONAL HELP./n/n
|
88 |
YOU ARE WRITING THE REPORT IN HTML USING A TEMPLATE.'''}]
|
89 |
|
|
|
|
|
|
|
|
|
|
|
90 |
|
91 |
-
llm = ChatOpenAI(model_name="gpt-4o-mini", temperature=0, openai_api_key=openai_api_key)
|
92 |
|
93 |
# Create the prompt template and the run statement when there are NOT issues
|
94 |
if not _issues and len(_items) > 0:
|
@@ -158,15 +160,22 @@ else:
|
|
158 |
"advice", "items", "topics", "issues", "technologies"])
|
159 |
run_statement = {"advice": _advice, "items": _items,
|
160 |
"topics": _topics, "issues": _issues, "technologies": _technologies}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
161 |
|
162 |
-
# Create the LLMChain
|
163 |
-
llm_chain = LLMChain(prompt=prompt, llm=llm)
|
164 |
|
165 |
-
# If there are no issues, remove the issues from the prompt
|
166 |
-
prompt_text = prompt.format(**run_statement)
|
167 |
-
|
168 |
-
# Run the LLMChain and return the output
|
169 |
-
out = llm_chain.run(**run_statement)
|
170 |
|
171 |
return out
|
172 |
|
|
|
11 |
from functools import wraps # for caching
|
12 |
import contextlib # for redirect_stdout
|
13 |
import tldextract
|
|
|
|
|
|
|
14 |
import requests
|
15 |
import streamlit as st
|
16 |
import pandas as pd
|
17 |
import streamlit.components.v1 as components
|
18 |
import json
|
19 |
import os
|
20 |
+
from openai import OpenAI
|
21 |
|
22 |
|
23 |
# ---------------------------------------------------------------------------- #
|
|
|
85 |
ADD AS LEARN MORE LINKS FOR THE FIRST TEXT BLOCK LINKS TO structured data https://wordlift.io/blog/en/entity/structured-data/ and schema.org https://wordlift.io/blog/en/entity/schema-org/ TO PROVIDE ADDITIONAL HELP./n/n
|
86 |
YOU ARE WRITING THE REPORT IN HTML USING A TEMPLATE.'''}]
|
87 |
|
88 |
+
client = OpenAI(api_key=openai_api_key)
|
89 |
+
|
90 |
+
# Construct messages for the chat API
|
91 |
+
messages = []
|
92 |
+
messages.extend(prefix_messages)
|
93 |
|
|
|
94 |
|
95 |
# Create the prompt template and the run statement when there are NOT issues
|
96 |
if not _issues and len(_items) > 0:
|
|
|
160 |
"advice", "items", "topics", "issues", "technologies"])
|
161 |
run_statement = {"advice": _advice, "items": _items,
|
162 |
"topics": _topics, "issues": _issues, "technologies": _technologies}
|
163 |
+
|
164 |
+
# Format the prompt
|
165 |
+
user_message = prompt.format(**run_statement)
|
166 |
+
messages.append({"role": "user", "content": user_message})
|
167 |
+
|
168 |
+
# Make the API call
|
169 |
+
try:
|
170 |
+
response = client.chat.completions.create(
|
171 |
+
model="gpt-4o",
|
172 |
+
messages=messages
|
173 |
+
)
|
174 |
+
out = response.choices[0].message.content
|
175 |
+
except Exception as e:
|
176 |
+
out = f"Sorry, there was an error with the OpenAI API: {e}"
|
177 |
|
|
|
|
|
178 |
|
|
|
|
|
|
|
|
|
|
|
179 |
|
180 |
return out
|
181 |
|