Update app.py
Browse files
app.py
CHANGED
@@ -28,12 +28,38 @@ def home():
|
|
28 |
def sentiment_analysis_ep(content: Text = None):
|
29 |
url = 'https://api.openai.com/v1/chat/completions'
|
30 |
prompt = '根据下面的文章,生成的“问题和回答”QA对,大于5个,以一行一个json格式({“question”:"xxx","answer":"xxx"})生成:\n'
|
31 |
-
print("content = \n", prompt + content.content)
|
32 |
messages = [{"role": "user", "content": prompt + content.content}]
|
33 |
data = {
|
34 |
"model": "gpt-3.5-turbo",
|
35 |
"messages": messages
|
36 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
result = requests.post(url=url,
|
38 |
data=json.dumps(data),
|
39 |
headers=headers
|
@@ -55,4 +81,4 @@ def embeddings_ep(content: Text = None):
|
|
55 |
data=json.dumps(data),
|
56 |
headers=headers
|
57 |
)
|
58 |
-
return JSONResponse(content=result.json())
|
|
|
28 |
def sentiment_analysis_ep(content: Text = None):
|
29 |
url = 'https://api.openai.com/v1/chat/completions'
|
30 |
prompt = '根据下面的文章,生成的“问题和回答”QA对,大于5个,以一行一个json格式({“question”:"xxx","answer":"xxx"})生成:\n'
|
|
|
31 |
messages = [{"role": "user", "content": prompt + content.content}]
|
32 |
data = {
|
33 |
"model": "gpt-3.5-turbo",
|
34 |
"messages": messages
|
35 |
}
|
36 |
+
print("messages = \n", messages)
|
37 |
+
result = requests.post(url=url,
|
38 |
+
data=json.dumps(data),
|
39 |
+
headers=headers
|
40 |
+
)
|
41 |
+
res = str(result.json()['choices'][0]['message']['content']).strip()
|
42 |
+
print('res:', res)
|
43 |
+
res = {'content': res}
|
44 |
+
return JSONResponse(content=res)
|
45 |
+
|
46 |
+
|
47 |
+
@app.post("/chat_pdf")
|
48 |
+
def chat_pdf_ep(content: Text = None):
|
49 |
+
url = 'https://api.openai.com/v1/chat/completions'
|
50 |
+
messages = [
|
51 |
+
{
|
52 |
+
"role": "system",
|
53 |
+
"content": "你是一个有用的助手,可以使用文章内容准确地回答问题。使用提供的文章来形成你的答案,但避免逐字复制文章。尽可能使用自己的话。准确、有用、简洁、清晰。"
|
54 |
+
}
|
55 |
+
]
|
56 |
+
obj = json.loads(content.content)
|
57 |
+
messages.append({"role": "user", "content": obj['question'] + '\n根据以下文章回答:\n' + obj['doc']})
|
58 |
+
data = {
|
59 |
+
"model": "gpt-3.5-turbo",
|
60 |
+
"messages": messages
|
61 |
+
}
|
62 |
+
print("messages = \n", messages)
|
63 |
result = requests.post(url=url,
|
64 |
data=json.dumps(data),
|
65 |
headers=headers
|
|
|
81 |
data=json.dumps(data),
|
82 |
headers=headers
|
83 |
)
|
84 |
+
return JSONResponse(content=result.json())
|