|
|
|
|
|
''' |
|
@File : spark_api.py |
|
@Time : 2023/09/24 11:00:46 |
|
@Author : Logan Zou |
|
@Version : 1.0 |
|
@Contact : loganzou0421@163.com |
|
@License : (C)Copyright 2017-2018, Liugroup-NLPR-CASIA |
|
@Desc : 启动服务为本地 API |
|
''' |
|
|
|
from fastapi import FastAPI |
|
from pydantic import BaseModel |
|
import os |
|
import sys |
|
from pathlib import Path |
|
|
|
|
|
current_dir = Path(__file__).resolve().parent |
|
project_root = current_dir.parent |
|
sys.path.append(str(project_root)) |
|
|
|
from qa_chain.QA_chain_self import QA_chain_self |
|
|
|
|
|
|
|
|
|
app = FastAPI() |
|
|
|
template = """使用以下上下文来回答最后的问题。如果你不知道答案,就说你不知道,不要试图编造答 |
|
案。最多使用三句话。尽量使答案简明扼要。总是在回答的最后说"谢谢你的提问!"。 |
|
{context} |
|
问题: {question} |
|
有用的回答:""" |
|
|
|
|
|
class Item(BaseModel): |
|
prompt : str |
|
model : str = "gpt-3.5-turbo" |
|
temperature : float = 0.1 |
|
if_history : bool = False |
|
|
|
api_key: str = None |
|
|
|
secret_key : str = None |
|
|
|
access_token: str = None |
|
|
|
appid : str = None |
|
|
|
Spark_api_secret : str = None |
|
|
|
Wenxin_secret_key : str = None |
|
|
|
db_path : str = "/Users/lta/Desktop/llm-universe/data_base/vector_db/chroma" |
|
|
|
file_path : str = "/Users/lta/Desktop/llm-universe/data_base/knowledge_db" |
|
|
|
prompt_template : str = template |
|
|
|
input_variables : list = ["context","question"] |
|
|
|
embedding : str = "m3e" |
|
|
|
top_k : int = 5 |
|
|
|
embedding_key : str = None |
|
|
|
@app.post("/") |
|
async def get_response(item: Item): |
|
|
|
|
|
if not item.if_history: |
|
|
|
|
|
if item.embedding_key == None: |
|
item.embedding_key = item.api_key |
|
chain = QA_chain_self(model=item.model, temperature=item.temperature, top_k=item.top_k, file_path=item.file_path, persist_path=item.db_path, |
|
appid=item.appid, api_key=item.api_key, embedding=item.embedding, template=template, Spark_api_secret=item.Spark_api_secret, Wenxin_secret_key=item.Wenxin_secret_key, embedding_key=item.embedding_key) |
|
|
|
response = chain.answer(question = item.prompt) |
|
|
|
return response |
|
|
|
|
|
else: |
|
return "API 不支持历史链" |