要借助于iOS的Siri定制个人助理,需要 1)基于LangChain实现简单的工具交互能力,包括联网能力;2)基于iOS的shortcuts定制一个助理。
1. LangChain的代码实现
import os
import ray
from ray import serve
from fastapi import FastAPI, Request
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.llms import OpenAI
from langchain.agents import Tool
from langchain.memory import ConversationBufferMemory
from langchain.chat_models import ChatOpenAI
from langchain.utilities import SerpAPIWrapper
from langchain.agents import initialize_agent
from langchain.tools.python.tool import PythonREPLTool
app = FastAPI()
@serve.deployment(route_prefix="/v1")
@serve.ingress(app)
class MyFastAPIDeployment5:
def __init__(self) -> None:
os.environ["OPENAI_API_KEY"] = "你的OpenAIKEY"
# 这里使用aido代理绕过GFW(放心,秘钥保证安全)
os.environ["OPENAI_API_BASE"] = "https://proxy.aido.ai/v1"
# 使用SerpAPI对接搜索引擎,注册链接:https://serpapi.com/dashboard
os.environ["SERPAPI_API_KEY"] = "你的SerpAPI的key"
search = SerpAPIWrapper()
llm=ChatOpenAI(temperature=0)
tools = [
Tool(
name = "Current Search",
func=search.run,
description="useful for when you need to answer questions about current events or the current state of the world. the input to this should be a single search term."
),
PythonREPLTool()
]
self.memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
self.agent = initialize_agent(tools, llm, agent="chat-conversational-react-description", verbose=True, memory = self.memory)
@app.post("/")
async def root(self, request: Request):
if request is None:
return "Hello, world!"
body = await request.json()
self.memory.clear()
return self.agent.run(body.get("q"))
serve.run(MyFastAPIDeployment5.bind())
python依赖安装ray、langchain、fastapi
# ray start --head
# python xxx.py
2. 定制iOS Shortcuts
可基于我这个来做改造:快捷指令
