File size: 1,244 Bytes
1809ff7
 
d057bd6
ad8b9b4
85b90d6
 
1809ff7
ad8b9b4
 
85b90d6
 
1809ff7
 
85b90d6
 
 
 
 
 
1809ff7
 
 
 
 
 
 
85b90d6
ad8b9b4
85b90d6
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from pydantic import BaseModel
from fastapi.responses import JSONResponse
import requests
import json

class Text(BaseModel):
    content: str = ""


app = FastAPI()

url = 'https://api.openai.com/v1/chat/completions'
headers = {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer ' + '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'
}


@app.get("/")
def home():
    html_content = open('index.html').read()
    return HTMLResponse(content=html_content, status_code=200)


@app.post("/qa_maker")
def sentiment_analysis_ep(content: Text = None):
    prompt = '根据下面的文章,生成的“问题和回答”QA对,大于5个,以一行一个json格式({“question”:"xxx","answer":"xxx"})生成:\n'
    messages = [{"role": "user", "content": prompt + content.content}]
    data = {
        "model": "gpt-3.5-turbo",
        "messages": messages
    }
    result = requests.post(url=url,
                           data=json.dumps(data),
                           headers=headers
                           )

    res = {'content': str(result.json()['choices'][0]['message']['content']).strip()}
    return JSONResponse(content=res)