souljoy commited on
Commit
85b90d6
1 Parent(s): e63bb76

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -8
app.py CHANGED
@@ -2,12 +2,21 @@ from fastapi import FastAPI
2
  from fastapi.responses import HTMLResponse
3
  from pydantic import BaseModel
4
  from fastapi.responses import JSONResponse
 
 
5
 
6
  class Text(BaseModel):
7
  content: str = ""
8
-
 
9
  app = FastAPI()
10
 
 
 
 
 
 
 
11
 
12
  @app.get("/")
13
  def home():
@@ -15,11 +24,18 @@ def home():
15
  return HTMLResponse(content=html_content, status_code=200)
16
 
17
 
18
- @app.get("/hello")
19
- def hello():
20
- return {"hello": "world"}
21
-
22
- @app.post("/sentiment-analysis")
23
  def sentiment_analysis_ep(content: Text = None):
24
- content = {"message": "hello world"}
25
- return JSONResponse(content=content)
 
 
 
 
 
 
 
 
 
 
 
 
2
  from fastapi.responses import HTMLResponse
3
  from pydantic import BaseModel
4
  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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'
18
+ }
19
+
20
 
21
  @app.get("/")
22
  def home():
 
24
  return HTMLResponse(content=html_content, status_code=200)
25
 
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",
33
+ "messages": messages
34
+ }
35
+ result = requests.post(url=url,
36
+ data=json.dumps(data),
37
+ headers=headers
38
+ )
39
+
40
+ res = {'content': str(result.json()['choices'][0]['message']['content']).strip()}
41
+ return JSONResponse(content=res)