Browse Source

测试openai api接口

gongyuan 9 months ago
parent
commit
9c00a4780d

+ 14 - 0
api_demo/v1/impl/AnswerImpl.py

@@ -0,0 +1,14 @@
+from fastapi import APIRouter
+from service_demo.impl.AnswerServiceImpl import get_answer, get_answers
+
+answer_router = APIRouter()
+
+# 查询参数通常用于传递额外的信息,比如搜索关键字、过滤条件等等。
+@answer_router.get("/getAnswer")
+async def getAnswer(question: str):
+    return get_answer(question)
+
+
+@answer_router.get("/getAnswerAll")
+async def getAnswerAll():
+    return get_answers()

BIN
api_demo/v1/impl/__pycache__/AnswerImpl.cpython-310.pyc


BIN
api_demo/v1/impl/__pycache__/BookImpl.cpython-310.pyc


BIN
api_demo/v1/impl/__pycache__/MusicImpl.cpython-310.pyc


+ 4 - 0
main.py

@@ -25,6 +25,7 @@
 from fastapi import FastAPI
 from api_demo.v1.impl.BookImpl import book_router
 from api_demo.v1.impl.MusicImpl import music_router
+from api_demo.v1.impl.AnswerImpl import answer_router
 
 app = FastAPI()
 
@@ -34,6 +35,9 @@ app.include_router(book_router, prefix="/books", tags=["books"])
 # 注册音乐搜索相关的路由
 app.include_router(music_router, prefix="/music", tags=["music"])
 
+# 注册answer搜索相关的路由
+app.include_router(answer_router, prefix="/answer", tags=["answer"])
+
 if __name__ == "__main__":
     import uvicorn
     uvicorn.run(app, host="127.0.0.1", port=8000)

+ 18 - 0
service_demo/impl/AnswerServiceImpl.py

@@ -0,0 +1,18 @@
+answers = [
+    {"id": 1, "question": "question1", "answer": "answer1"},
+    {"id": 2, "question": "question2", "answer": "answer2"},
+    {"id": 3, "question": "question3", "answer": "answer3"}
+]
+
+
+def get_answer(question: str):
+    for answer in answers:
+        if answer["question"] == question:
+            return answer
+    return {"message": "question not found"}
+
+
+def get_answers():
+    return answers
+
+#去调用chatgpt搜索答案

BIN
service_demo/impl/__pycache__/AnswerServiceImpl.cpython-310.pyc


BIN
service_demo/impl/__pycache__/BookServiceImpl.cpython-310.pyc


BIN
service_demo/impl/__pycache__/MusicServiceImpl.cpython-310.pyc