Spaces:
Runtime error
Runtime error
| from fastapi import FastAPI,Request | |
| from fastapi.responses import StreamingResponse | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from service.vector_store import vector_store | |
| from service.api import api | |
| from service.scheduler import Scheduler | |
| from pydantic import BaseModel | |
| app = FastAPI() | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], # 允许所有来源 | |
| allow_credentials=True, | |
| allow_methods=["*"], # 允许所有方法 | |
| allow_headers=["*"], # 允许所有头 | |
| ) | |
| scheduler = Scheduler() | |
| scheduler.run() | |
| def read_root(): | |
| return {"Hello": "World!"} | |
| def search(request: Request, inputs: str): | |
| client_host = request.client.host | |
| x_forwarded_for = request.headers.get("x-forwarded-for") | |
| if x_forwarded_for: | |
| client_host = x_forwarded_for.split(",")[0] # 取X-Forwarded-For头的第一个值 | |
| print(f"client_host: {client_host}") | |
| return vector_store.search(inputs) | |
| ## FILECOIN WEB COMPLETE API | |
| def get_suggestion(inputs: str): | |
| return api.get_suggestion(inputs) | |
| ## FILECOIN WEB ASNWER API | |
| async def get_answer(inputs: str): | |
| return StreamingResponse(api.get_answer(inputs)) | |
| class AnswerRequest(BaseModel): | |
| inputs: str | |
| async def post_answer(request: AnswerRequest): | |
| # 假设 api.get_answer() 需要 description 和 link 两个参数 | |
| response = api.get_answer_v2(request.inputs) | |
| return StreamingResponse(response) |