Spaces:
Paused
Paused
netman19731
commited on
Commit
•
0ccd0c9
1
Parent(s):
f38ddbf
Update app.py
Browse files
app.py
CHANGED
@@ -1,48 +1,64 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
|
|
3 |
from http import HTTPStatus
|
4 |
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
-
print(response.text)
|
33 |
-
if response.status_code == HTTPStatus.OK:
|
34 |
-
response=json.loads(response.text)
|
35 |
-
messages.append({'role': response["output"]["choices"][0]["message"]["role"],
|
36 |
-
'content': response["output"]["choices"][0]["message"]["content"]})
|
37 |
-
else:
|
38 |
-
print('Request id: %s, Status code: %s, error code: %s, error message: %s' % (
|
39 |
-
response.request_id, response.status_code,
|
40 |
-
response.code, response.message
|
41 |
-
))
|
42 |
-
|
43 |
-
partial_message = ""
|
44 |
-
partial_message = partial_message+response["output"]["choices"][0]["message"]["content"]
|
45 |
-
return partial_message
|
46 |
-
|
47 |
-
|
48 |
-
gr.ChatInterface(predict).launch()
|
|
|
1 |
+
import os
|
2 |
import gradio as gr
|
3 |
import requests
|
4 |
+
import dashscope
|
5 |
from http import HTTPStatus
|
6 |
import json
|
7 |
+
from langchain.llms import Tongyi
|
8 |
+
from langchain import hub
|
9 |
+
from langchain_community.tools.tavily_search import TavilySearchResults
|
10 |
+
from langchain.tools import tool
|
11 |
+
from langchain.embeddings import TensorflowHubEmbeddings
|
12 |
+
from pinecone import Pinecone, ServerlessSpec
|
13 |
+
from langchain.vectorstores import Pinecone as Pinecone_VectorStore
|
14 |
+
from langchain.tools.retriever import create_retriever_tool
|
15 |
+
from langchain.agents import AgentExecutor,create_react_agent
|
16 |
|
17 |
+
os.environ['TAVILY_API_KEY'] = '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'
|
18 |
+
@tool
|
19 |
+
def tqyb(query: str) -> str:
|
20 |
+
"""这是天气预报api,示例query=北京"""
|
21 |
+
url=f"https://api.seniverse.com/v3/weather/now.json?key=SWtPLxs4A2GhenWC-&location={query}&language=zh-Hans&unit=c"
|
22 |
+
response = requests.get(url)
|
23 |
+
# 检查请求是否成功
|
24 |
+
if response.status_code == 200:
|
25 |
+
res=response.json()
|
26 |
+
return res # 假设API返回的是JSON格式数据
|
27 |
+
else:
|
28 |
+
return f"请求失败,状态码:{response.status_code}"
|
29 |
|
30 |
+
|
31 |
+
llm = Tongyi(dashscope_api_key="sk-78c45d761ed04af2b965b43cd522108b",model="qwen-72b-chat")
|
32 |
+
prompt = hub.pull("hwchase17/react")
|
33 |
+
search = TavilySearchResults(max_results=1)
|
34 |
+
|
35 |
+
embeddings = TensorflowHubEmbeddings()
|
36 |
+
pc = Pinecone(api_key='3538cd3c-eca8-4c61-9463-759f5ea65b10')
|
37 |
+
index = pc.Index("myindex")
|
38 |
+
vectorstore = Pinecone_VectorStore(index, embeddings.embed_query, "text")
|
39 |
+
db=vectorstore.as_retriever()
|
40 |
+
retriever_tool = create_retriever_tool(
|
41 |
+
db,
|
42 |
+
"shuangcheng_search",
|
43 |
+
"关于双城的区情信息检索工具,如果问题与双城的区情有关,你必须使用这个工具!",
|
44 |
+
)
|
45 |
+
|
46 |
+
tools = [search,tqyb,retriever_tool]
|
47 |
+
agent = create_react_agent(llm, tools, prompt)
|
48 |
+
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
|
49 |
+
|
50 |
+
async def predict(question):
|
51 |
+
que={"input":question}
|
52 |
+
res=agent_executor.invoke(que)
|
53 |
+
if res:
|
54 |
+
return(res["output"])
|
55 |
+
else:print("不好意思,出了一个小问题,请联系我的微信:13603634456")
|
56 |
|
57 |
+
|
58 |
+
gr.Interface(
|
59 |
+
predict,inputs="textbox",
|
60 |
+
outputs="textbox",
|
61 |
+
title="定制版AI专家BOT",
|
62 |
+
description="这是一个定制版的AI专家BOT,你可以通过输入问题,让AI为你回答。\n目前提供三个示例工具:\n1.天气预报(函数调用API)\n2.双城区情检索(增强型检索RAG)\n3.搜索引擎").launch()
|
63 |
+
|
64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|