langchain zero to hero agents
1.0.0
Mulailah dengan Agen LangChain, bagian dari seri zero-to-hero.
Unduh dan instal Puisi.
Siapkan lingkungan Puisi:
?
poetry init --no-interaction --python= " ^3.11 " --dependency=langchain --dependency=langchain-openai --dependency=langchainhub --dependency= " langserve[all] " --dependency=duckduckgo-search
poetry install
export OPENAI_API_KEY=...
langchain_zero_to_hero_agents/src/main.py
dan buat agen sederhana (kode dimodifikasi dari Buku Masak Agen LangChain) from langchain import hub
from langchain . agents import AgentExecutor , tool
from langchain . agents . output_parsers import XMLAgentOutputParser
from langchain_openai import ChatOpenAI
from langchain_community . tools import DuckDuckGoSearchResults
#######################
# LangChain Agent Code
#######################
model = ChatOpenAI ( model = "gpt-3.5-turbo" , temperature = 0 , streaming = True )
@ tool
def search ( query : str ) -> str :
"""Search things about current events."""
search = DuckDuckGoSearchResults ()
return search . run ( query )
tool_list = [ search ]
prompt = hub . pull ( "hwchase17/xml-agent-convo" )
def convert_intermediate_steps ( intermediate_steps ):
log = ""
for action , observation in intermediate_steps :
log += (
f"<tool> { action . tool } </tool><tool_input> { action . tool_input } "
f"</tool_input><observation> { observation } </observation>"
)
return log
def convert_tools ( tools ):
return " n " . join ([ f" { tool . name } : { tool . description } " for tool in tools ])
agent = (
{
"input" : lambda x : x [ "input" ],
"agent_scratchpad" : lambda x : convert_intermediate_steps (
x [ "intermediate_steps" ]
),
}
| prompt . partial ( tools = convert_tools ( tool_list ))
| model . bind ( stop = [ "</tool_input>" , "</final_answer>" ])
| XMLAgentOutputParser ()
)
agent_executor = AgentExecutor ( agent = agent , tools = tool_list , verbose = True )
if __name__ == "__main__" :
print ( agent_executor . invoke ({ "input" : "whats the weather in New york?" }))
♻️
poetry run python langchain_zero_to_hero_agents/src/main.py
from fastapi import FastAPI
from langchain . pydantic_v1 import BaseModel
from langserve import add_routes
from typing import Any
#######################
# LangChain Agent Code
#######################
# ...
######################
# LangServe API Code
######################
class Input ( BaseModel ):
input : str
class Output ( BaseModel ):
output : Any
app = FastAPI (
title = "DuckDuckGo Agent" ,
version = "1.0" ,
description = "API for accessing a simple LangChain agent that can query the web with DuckDuckGo." ,
)
add_routes (
app ,
agent_executor . with_types ( input_type = Input , output_type = Output ). with_config (
{ "run_name" : "agent" }
),
path = "/agent"
)
if __name__ == "__main__" :
import uvicorn
uvicorn . run ( app , host = "localhost" , port = 8000 )
♻️
poetry run python langchain_zero_to_hero_agents/src/main.py
Kunjungi http://localhost:8000/agent/playground/ untuk mengakses UI sederhana untuk berinteraksi dengan agen Anda.
Buat skrip pengujian ( langchain_zero_to_hero_agents/tests/test.py
) untuk bereksperimen mengakses API Anda melalui Python:
import requests
response = requests . post (
"http://localhost:8000/agent/invoke" ,
json = { 'input' : { "input" : "what is the weather in new york" }}
)
print ( response . json ())
?
poetry run python langchain_zero_to_hero_agents/tests/test.py
MIT