Example inspired from the LangChain doc
Prerequisites
This example has extra dependencies. You can install them with:
pip install chainlit langchain openai google-search-results
Code
from langchain.chains import LLMMathChain
from langchain.llms.openai import OpenAI
from langchain.chat_models import ChatOpenAI
from langchain.utilities.serpapi import SerpAPIWrapper
from langchain.agents import initialize_agent, Tool, AgentExecutor
import os
import chainlit as cl
os.environ["OPENAI_API_KEY"] = "OPENAI_API_KEY"
os.environ["SERPAPI_API_KEY"] = "SERPAPI_API_KEY"
@cl.on_chat_start
def start():
llm = ChatOpenAI(temperature=0, streaming=True)
llm1 = OpenAI(temperature=0, streaming=True)
search = SerpAPIWrapper()
llm_math_chain = LLMMathChain.from_llm(llm=llm, verbose=True)
tools = [
Tool(
name="Search",
func=search.run,
description="useful for when you need to answer questions about current events. You should ask targeted questions",
),
Tool(
name="Calculator",
func=llm_math_chain.run,
description="useful for when you need to answer questions about math",
),
]
agent = initialize_agent(
tools, llm1, agent="chat-zero-shot-react-description", verbose=True
)
cl.user_session.set("agent", agent)
@cl.on_message
async def main(message: cl.Message):
agent = cl.user_session.get("agent")
cb = cl.LangchainCallbackHandler(stream_final_answer=True)
await cl.make_async(agent.run)(message.content, callbacks=[cb])
Try it out
You can ask questions like What is the Paris weather forecast for tomorrow? How does it compare to today's?
.