Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,38 @@
|
|
| 1 |
from fastapi import FastAPI
|
|
|
|
|
|
|
| 2 |
|
| 3 |
app = FastAPI()
|
| 4 |
|
| 5 |
@app.get("/")
|
| 6 |
-
def greet_json():
|
| 7 |
return {"Hello": "World!123456789"}
|
| 8 |
|
| 9 |
@app.post("/v1/completions")
|
| 10 |
async def completions():
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
|
| 5 |
app = FastAPI()
|
| 6 |
|
| 7 |
@app.get("/")
|
| 8 |
+
async def greet_json():
|
| 9 |
return {"Hello": "World!123456789"}
|
| 10 |
|
| 11 |
@app.post("/v1/completions")
|
| 12 |
async def completions():
|
| 13 |
+
url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro-latest:generateContent?key=AIzaSyAw-zbI35Qd9Pan93RAiWyJjDm-nbPTHaQ"
|
| 14 |
+
# 构造请求头
|
| 15 |
+
headers = {
|
| 16 |
+
'Content-Type': 'application/json'
|
| 17 |
+
}
|
| 18 |
+
# 构造请求体
|
| 19 |
+
data = {
|
| 20 |
+
"contents": [{
|
| 21 |
+
"role": "user",
|
| 22 |
+
"parts":[{
|
| 23 |
+
"text": "你好"
|
| 24 |
+
}]
|
| 25 |
+
}]
|
| 26 |
+
}
|
| 27 |
+
# 发送 POST 请求
|
| 28 |
+
response = requests.post(url, headers=headers, data=json.dumps(data))
|
| 29 |
|
| 30 |
+
# 解析响应
|
| 31 |
+
if response.status_code == 200:
|
| 32 |
+
content = response.json()
|
| 33 |
+
return "ok"
|
| 34 |
+
print(content['candidates'][0]['content'])
|
| 35 |
+
else:
|
| 36 |
+
print(f"Error: {response.status_code}, {response.text}")
|
| 37 |
+
return f"Error: {response.status_code}, {response.text}"
|
| 38 |
+
# return {"Hello": "POST World"}
|