lmt commited on
Commit
4bf9eff
·
1 Parent(s): 4f71b16

提交api

Browse files
Files changed (2) hide show
  1. main.py +49 -1
  2. requirements.txt +0 -1
main.py CHANGED
@@ -1,7 +1,55 @@
 
1
  from fastapi import FastAPI
 
 
 
 
 
2
 
3
  app = FastAPI()
4
 
 
5
  @app.get("/")
6
  def read_root():
7
- return {"Hello": "World!"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
  from fastapi import FastAPI
3
+ import requests
4
+
5
+ api_key = os.environ.get('api_key')
6
+ initial_prompt = "You are a helpful assistant."
7
+ API_URL = "https://api.openai.com/v1/chat/completions"
8
 
9
  app = FastAPI()
10
 
11
+
12
  @app.get("/")
13
  def read_root():
14
+ return {"Hello": "World!"}
15
+
16
+
17
+ @app.post("/api/chat")
18
+ def chat(input, history: list):
19
+ history.append(construct_user(input))
20
+ res = get_response(initial_prompt, history)
21
+ return res
22
+
23
+
24
+ def get_response(system_prompt, history):
25
+ headers = {
26
+ "Content-Type": "application/json",
27
+ "Authorization": f"Bearer {api_key}"
28
+ }
29
+
30
+ history = [construct_system(system_prompt), *history]
31
+
32
+ payload = {
33
+ "model": "gpt-3.5-turbo",
34
+ "messages": history, # [{"role": "user", "content": f"{inputs}"}],
35
+ }
36
+
37
+ response = requests.post(API_URL, headers=headers,
38
+ json=payload, stream=True, timeout=timeout)
39
+ return response
40
+
41
+
42
+ def construct_user(text):
43
+ return construct_text("user", text)
44
+
45
+
46
+ def construct_system(text):
47
+ return construct_text("system", text)
48
+
49
+
50
+ def construct_assistant(text):
51
+ return construct_text("assistant", text)
52
+
53
+
54
+ def construct_text(role, text):
55
+ return {"role": role, "content": text}
requirements.txt CHANGED
@@ -1,4 +1,3 @@
1
  fastapi==0.74.*
2
  requests==2.27.*
3
- sentencepiece==0.1.*
4
  uvicorn[standard]==0.17.*
 
1
  fastapi==0.74.*
2
  requests==2.27.*
 
3
  uvicorn[standard]==0.17.*