Update app.py
Browse files
app.py
CHANGED
@@ -5,13 +5,13 @@ from fastapi.responses import JSONResponse
|
|
5 |
import requests
|
6 |
import json
|
7 |
|
|
|
8 |
class Text(BaseModel):
|
9 |
content: str = ""
|
10 |
|
11 |
|
12 |
app = FastAPI()
|
13 |
|
14 |
-
url = 'https://api.openai.com/v1/chat/completions'
|
15 |
headers = {
|
16 |
'Content-Type': 'application/json',
|
17 |
'Authorization': 'Bearer ' + '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'
|
@@ -26,7 +26,9 @@ def home():
|
|
26 |
|
27 |
@app.post("/qa_maker")
|
28 |
def sentiment_analysis_ep(content: Text = None):
|
|
|
29 |
prompt = '根据下面的文章,生成的“问题和回答”QA对,大于5个,以一行一个json格式({“question”:"xxx","answer":"xxx"})生成:\n'
|
|
|
30 |
messages = [{"role": "user", "content": prompt + content.content}]
|
31 |
data = {
|
32 |
"model": "gpt-3.5-turbo",
|
@@ -36,6 +38,21 @@ def sentiment_analysis_ep(content: Text = None):
|
|
36 |
data=json.dumps(data),
|
37 |
headers=headers
|
38 |
)
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
import requests
|
6 |
import json
|
7 |
|
8 |
+
|
9 |
class Text(BaseModel):
|
10 |
content: str = ""
|
11 |
|
12 |
|
13 |
app = FastAPI()
|
14 |
|
|
|
15 |
headers = {
|
16 |
'Content-Type': 'application/json',
|
17 |
'Authorization': 'Bearer ' + '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'
|
|
|
26 |
|
27 |
@app.post("/qa_maker")
|
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",
|
|
|
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("/embeddings")
|
48 |
+
def embeddings(content: Text = None):
|
49 |
+
url = 'https://api.openai.com/v1/embeddings'
|
50 |
+
data = {
|
51 |
+
"model": "text-embedding-ada-002",
|
52 |
+
"input": content.content
|
53 |
+
}
|
54 |
+
result = requests.post(url=url,
|
55 |
+
data=json.dumps(data),
|
56 |
+
headers=headers
|
57 |
+
)
|
58 |
+
return JSONResponse(content=result.json())
|