Spaces:
Runtime error
Runtime error
jiarongqiu commited on
Commit ·
9ebbf3c
1
Parent(s): 6456be6
answer 2
Browse files- .gitignore +1 -0
- main.py +10 -1
- service/api.py +27 -2
- service/llm.py +19 -0
.gitignore
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
*.ipynb
|
| 2 |
script/
|
| 3 |
data/
|
|
|
|
| 1 |
+
.DS_Store
|
| 2 |
*.ipynb
|
| 3 |
script/
|
| 4 |
data/
|
main.py
CHANGED
|
@@ -39,4 +39,13 @@ def get_suggestion(inputs: str):
|
|
| 39 |
|
| 40 |
@app.get("/api/answer")
|
| 41 |
async def get_answer(inputs: str):
|
| 42 |
-
return StreamingResponse(api.get_answer(inputs))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
@app.get("/api/answer")
|
| 41 |
async def get_answer(inputs: str):
|
| 42 |
+
return StreamingResponse(api.get_answer(inputs))
|
| 43 |
+
|
| 44 |
+
class AnswerRequest(BaseModel):
|
| 45 |
+
inputs: str
|
| 46 |
+
|
| 47 |
+
@app.post("/api/answer_v2")
|
| 48 |
+
async def post_answer(request: AnswerRequest):
|
| 49 |
+
# 假设 api.get_answer() 需要 description 和 link 两个参数
|
| 50 |
+
response = api.get_answer_v2(request.inputs)
|
| 51 |
+
return StreamingResponse(response)
|
service/api.py
CHANGED
|
@@ -3,7 +3,7 @@ import time
|
|
| 3 |
import json
|
| 4 |
import requests
|
| 5 |
from typing import Any
|
| 6 |
-
from .llm import search_bot
|
| 7 |
from .vector_store import vector_store
|
| 8 |
# from .logger import logger
|
| 9 |
from util.time import TimeUtil
|
|
@@ -65,7 +65,32 @@ class API():
|
|
| 65 |
text += i
|
| 66 |
yield i
|
| 67 |
database.write({"query":query,"api":'search',"result":text},dir=Dir.API)
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
def get_filecoin_price(self):
|
| 71 |
url="https://api.spacescope.io/v2/price/realtime"
|
|
|
|
| 3 |
import json
|
| 4 |
import requests
|
| 5 |
from typing import Any
|
| 6 |
+
from .llm import search_bot,search_bot_v2
|
| 7 |
from .vector_store import vector_store
|
| 8 |
# from .logger import logger
|
| 9 |
from util.time import TimeUtil
|
|
|
|
| 65 |
text += i
|
| 66 |
yield i
|
| 67 |
database.write({"query":query,"api":'search',"result":text},dir=Dir.API)
|
| 68 |
+
|
| 69 |
+
def get_answer_v2(self,query):
|
| 70 |
+
docs = vector_store.search(query)
|
| 71 |
+
source = []
|
| 72 |
+
context = []
|
| 73 |
+
for doc in docs:
|
| 74 |
+
if doc.metadata['score']<0.2:
|
| 75 |
+
continue
|
| 76 |
+
if 'title_llm' in doc.metadata:
|
| 77 |
+
source.append({
|
| 78 |
+
"name":doc.metadata.get('title_llm',''),
|
| 79 |
+
"url":doc.metadata.get('source',''),
|
| 80 |
+
"description":doc.metadata.get('description_llm','')
|
| 81 |
+
})
|
| 82 |
+
description = doc.metadata.get('description','')
|
| 83 |
+
links = doc.metadata.get('source','')
|
| 84 |
+
context.append(f"<context>Description: {description}\nLinks: {links}\n<context/>")
|
| 85 |
+
# yield json.dumps(source)
|
| 86 |
+
# print(context)
|
| 87 |
+
context = "\n".join(context)
|
| 88 |
+
answer = search_bot_v2(query,context,stream=True)
|
| 89 |
+
text = ""
|
| 90 |
+
for i in answer:
|
| 91 |
+
text += i
|
| 92 |
+
yield i
|
| 93 |
+
database.write({"query":query,"api":'answer_v2',"result":text},dir=Dir.API)
|
| 94 |
|
| 95 |
def get_filecoin_price(self):
|
| 96 |
url="https://api.spacescope.io/v2/price/realtime"
|
service/llm.py
CHANGED
|
@@ -70,5 +70,24 @@ class SummerizeBot(Bot):
|
|
| 70 |
def __init__(self):
|
| 71 |
super().__init__(template=self.TEMPLATE)
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
search_bot = SearchBot()
|
|
|
|
| 74 |
summerize_bot = SummerizeBot()
|
|
|
|
| 70 |
def __init__(self):
|
| 71 |
super().__init__(template=self.TEMPLATE)
|
| 72 |
|
| 73 |
+
class SearchBotV2(Bot):
|
| 74 |
+
TEMPLATE = """
|
| 75 |
+
You are an expert tasked with summarizing search results and answering questions about FileCoin.
|
| 76 |
+
Generate a comprehensive and informative answer of 80 words or less for the
|
| 77 |
+
given question based solely on the provided search results in the context. You must
|
| 78 |
+
only use information from the provided search results. Include the most relevant link from the search results
|
| 79 |
+
to support your answer.
|
| 80 |
+
|
| 81 |
+
Below are the structured search results retrieved from a knowledge bank. Each entry consists of a description and associated links.
|
| 82 |
+
|
| 83 |
+
{context}
|
| 84 |
+
|
| 85 |
+
Your task is to provide a precise and concise response to the question, and also include the most relevant link from the search results that substantiates your answer.
|
| 86 |
+
"""
|
| 87 |
+
|
| 88 |
+
def __init__(self):
|
| 89 |
+
super().__init__(self.TEMPLATE)
|
| 90 |
+
|
| 91 |
search_bot = SearchBot()
|
| 92 |
+
search_bot_v2 = SearchBotV2()
|
| 93 |
summerize_bot = SummerizeBot()
|