Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,8 @@ import requests
|
|
| 6 |
import json
|
| 7 |
import openai
|
| 8 |
import time
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
class Text(BaseModel):
|
| 12 |
content: str = ""
|
|
@@ -30,7 +31,7 @@ def home():
|
|
| 30 |
@app.post("/qa_maker")
|
| 31 |
def sentiment_analysis_ep(content: Text = None):
|
| 32 |
url = 'https://api.openai.com/v1/chat/completions'
|
| 33 |
-
prompt = '
|
| 34 |
messages = [{"role": "user", "content": prompt + content.content}]
|
| 35 |
data = {
|
| 36 |
"model": "gpt-3.5-turbo",
|
|
@@ -53,18 +54,18 @@ def chat_pdf_ep(content: Text = None):
|
|
| 53 |
messages = [
|
| 54 |
{
|
| 55 |
"role": "system",
|
| 56 |
-
"content": "
|
| 57 |
}
|
| 58 |
]
|
| 59 |
obj = json.loads(content.content)
|
| 60 |
-
messages.append({"role": "system", "content": "
|
| 61 |
history = obj['history']
|
| 62 |
for his in history:
|
| 63 |
messages.append({"role": "user", "content": his[0]})
|
| 64 |
messages.append({"role": "assistant", "content": his[1]})
|
| 65 |
messages.append({"role": "user", "content": obj['question']})
|
| 66 |
data = {
|
| 67 |
-
"model": "
|
| 68 |
"messages": messages
|
| 69 |
}
|
| 70 |
print("messages = \n", messages)
|
|
@@ -84,11 +85,11 @@ def sale_ep(content: Text = None):
|
|
| 84 |
messages = [
|
| 85 |
{
|
| 86 |
"role": "system",
|
| 87 |
-
"content": "
|
| 88 |
}
|
| 89 |
]
|
| 90 |
obj = json.loads(content.content)
|
| 91 |
-
messages.append({"role": "system", "content": "
|
| 92 |
history = obj['history']
|
| 93 |
for his in history:
|
| 94 |
messages.append({"role": "user", "content": his[0]})
|
|
@@ -172,6 +173,22 @@ def embeddings_ep(content: Text = None):
|
|
| 172 |
return JSONResponse(content=result.json())
|
| 173 |
|
| 174 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
@app.post("/create_image")
|
| 176 |
def create_image_ep(content: Text = None):
|
| 177 |
url = 'https://api.openai.com/v1/images/generations'
|
|
|
|
| 6 |
import json
|
| 7 |
import openai
|
| 8 |
import time
|
| 9 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 10 |
+
import langchain
|
| 11 |
|
| 12 |
class Text(BaseModel):
|
| 13 |
content: str = ""
|
|
|
|
| 31 |
@app.post("/qa_maker")
|
| 32 |
def sentiment_analysis_ep(content: Text = None):
|
| 33 |
url = 'https://api.openai.com/v1/chat/completions'
|
| 34 |
+
prompt = 'According to the article below, generate "question and answer" QA pairs, greater than 5, in a json format per line({“question”:"xxx","answer":"xxx"})generate:\n'
|
| 35 |
messages = [{"role": "user", "content": prompt + content.content}]
|
| 36 |
data = {
|
| 37 |
"model": "gpt-3.5-turbo",
|
|
|
|
| 54 |
messages = [
|
| 55 |
{
|
| 56 |
"role": "system",
|
| 57 |
+
"content": "You are a useful assistant to answer questions accurately using the content of the article."
|
| 58 |
}
|
| 59 |
]
|
| 60 |
obj = json.loads(content.content)
|
| 61 |
+
messages.append({"role": "system", "content": "Article content:\n" + obj['doc']})
|
| 62 |
history = obj['history']
|
| 63 |
for his in history:
|
| 64 |
messages.append({"role": "user", "content": his[0]})
|
| 65 |
messages.append({"role": "assistant", "content": his[1]})
|
| 66 |
messages.append({"role": "user", "content": obj['question']})
|
| 67 |
data = {
|
| 68 |
+
"model": "text-davinci-003",
|
| 69 |
"messages": messages
|
| 70 |
}
|
| 71 |
print("messages = \n", messages)
|
|
|
|
| 85 |
messages = [
|
| 86 |
{
|
| 87 |
"role": "system",
|
| 88 |
+
"content": "You are a useful assistant to answer questions accurately using the content of the article"
|
| 89 |
}
|
| 90 |
]
|
| 91 |
obj = json.loads(content.content)
|
| 92 |
+
messages.append({"role": "system", "content": "Article content:\n" + obj['doc']})
|
| 93 |
history = obj['history']
|
| 94 |
for his in history:
|
| 95 |
messages.append({"role": "user", "content": his[0]})
|
|
|
|
| 173 |
return JSONResponse(content=result.json())
|
| 174 |
|
| 175 |
|
| 176 |
+
|
| 177 |
+
@app.post("/embedd")
|
| 178 |
+
def embed(content: Text = None):
|
| 179 |
+
url = 'https://api.openai.com/v1/embeddings'
|
| 180 |
+
data = {
|
| 181 |
+
"model": "text-embedding-ada-002",
|
| 182 |
+
"input": content.content
|
| 183 |
+
}
|
| 184 |
+
result = requests.post(url=url,
|
| 185 |
+
data=json.dumps(data),
|
| 186 |
+
headers=headers
|
| 187 |
+
)
|
| 188 |
+
embeddings = OpenAIEmbeddings(openai_api_key= key)
|
| 189 |
+
return key
|
| 190 |
+
|
| 191 |
+
|
| 192 |
@app.post("/create_image")
|
| 193 |
def create_image_ep(content: Text = None):
|
| 194 |
url = 'https://api.openai.com/v1/images/generations'
|