Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +70 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import faiss
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import openai
|
| 5 |
+
from langchain import SerpAPIWrapper, FAISS, InMemoryDocstore
|
| 6 |
+
from langchain.chat_models import ChatOpenAI
|
| 7 |
+
from langchain.embeddings import OpenAIEmbeddings
|
| 8 |
+
from langchain.tools import Tool, WriteFileTool, ReadFileTool
|
| 9 |
+
from langchain_experimental.autonomous_agents import AutoGPT
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
openai.api_base = "https://api.zyai.online/v1"
|
| 13 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
class AutoGPTTool:
|
| 17 |
+
def __init__(self):
|
| 18 |
+
self.search = SerpAPIWrapper()
|
| 19 |
+
self.tools = [
|
| 20 |
+
Tool(
|
| 21 |
+
name="search",
|
| 22 |
+
func=self.search.run,
|
| 23 |
+
description="useful for when you need to answer questions about current events. You should ask targeted questions",
|
| 24 |
+
),
|
| 25 |
+
WriteFileTool(),
|
| 26 |
+
ReadFileTool(),
|
| 27 |
+
]
|
| 28 |
+
|
| 29 |
+
self.embeddings_model = OpenAIEmbeddings()
|
| 30 |
+
self.embedding_size = 1536
|
| 31 |
+
self.index = faiss.IndexFlatL2(self.embedding_size)
|
| 32 |
+
self.vectorstore = FAISS(
|
| 33 |
+
self.embeddings_model.embed_query,
|
| 34 |
+
self.index,
|
| 35 |
+
InMemoryDocstore({}),
|
| 36 |
+
{},
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
self.agent = AutoGPT.from_llm_and_tools(
|
| 40 |
+
ai_name="小Y",
|
| 41 |
+
ai_role="Assistant",
|
| 42 |
+
tools=self.tools,
|
| 43 |
+
llm=ChatOpenAI(temperature=0),
|
| 44 |
+
memory=self.vectorstore.as_retriever(),
|
| 45 |
+
)
|
| 46 |
+
self.agent.chain.verbose = True
|
| 47 |
+
|
| 48 |
+
def process_question(self, question):
|
| 49 |
+
return self.agent.run([question])
|
| 50 |
+
|
| 51 |
+
def setup_gradio_interface(self):
|
| 52 |
+
iface = gr.Interface(
|
| 53 |
+
fn=self.process_question,
|
| 54 |
+
inputs=[gr.Textbox(lines=5, label="问题", placeholder="请输入问题...")],
|
| 55 |
+
outputs=[gr.Textbox(lines=5, label="答案")],
|
| 56 |
+
title="ChatAutoGPT助理",
|
| 57 |
+
description="我是您的ChatAutoGPT助理:小Y,让我们开始聊天吧~",
|
| 58 |
+
theme="soft",
|
| 59 |
+
examples=["2024年1月9日北京的天气怎么样?", "2024年欧洲杯举办地在哪?",
|
| 60 |
+
"Auto-GPT 是什么?把结果写到autogpt.txt文件中"],
|
| 61 |
+
allow_flagging="never"
|
| 62 |
+
)
|
| 63 |
+
return iface
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
if __name__ == "__main__":
|
| 67 |
+
# 使用示例
|
| 68 |
+
autogpt_tool = AutoGPTTool()
|
| 69 |
+
gradio_interface = autogpt_tool.setup_gradio_interface()
|
| 70 |
+
gradio_interface.launch(server_name="0.0.0.0")
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openai==v0.28.1
|
| 2 |
+
langchain
|