Aracılar için özel araçlar

LangChain ile LLM Uygulamaları Geliştirme

Jonathan Bennion

AI Engineer & LangChain Contributor

Araç biçimleri

from langchain_community.agent_toolkits.load_tools import load_tools

tools = load_tools(["llm-math"], llm=llm)

print(tools[0].name)
Hesap Makinesi
print(tools[0].description)
Matematikle ilgili soruları yanıtlamanız gerektiğinde kullanışlıdır.
  • LLM/aracı tarafından ne zaman çağrılacağını belirlemek için bağlam olarak kullanılır
LangChain ile LLM Uygulamaları Geliştirme

Araç biçimleri

print(tools[0].return_direct)
False
LangChain ile LLM Uygulamaları Geliştirme

Özel bir fonksiyon tanımlama

def financial_report(company_name: str, revenue: int, expenses: int) -> str:
    """Generate a financial report for a company that calculates net income."""
    net_income = revenue - expenses

    report = f"Financial Report for {company_name}:\n"
    report += f"Revenue: ${revenue}\n"
    report += f"Expenses: ${expenses}\n"
    report += f"Net Income: ${net_income}\n"
    return report
LangChain ile LLM Uygulamaları Geliştirme

Fonksiyonu çağırma

print(financial_report(company_name="LemonadeStand", revenue=100, expenses=50))
LemonadeStand için Finansal Rapor:
Gelir: $100
Gider: $50
Net Gelir: $50
LangChain ile LLM Uygulamaları Geliştirme

Fonksiyonlardan araçlara

from langchain_core.tools import tool

@tool
def financial_report(company_name: str, revenue: int, expenses: int) -> str: """Generate a financial report for a company that calculates net income.""" net_income = revenue - expenses report = f"Financial Report for {company_name}:\n" report += f"Revenue: ${revenue}\n" report += f"Expenses: ${expenses}\n" report += f"Net Income: ${net_income}\n" return report
LangChain ile LLM Uygulamaları Geliştirme

Yeni aracımızı incelemek

print(financial_report.name)

print(financial_report.description)
print(financial_report.return_direct)
print(financial_report.args)
financial_report

Net geliri hesaplayan bir şirket için finansal rapor oluşturun.
False
{'company_name': {'title': 'Company Name', 'type': 'string'}, 'revenue': {'title': 'Revenue', 'type': 'integer'}, 'expenses': {'title': 'Expenses', 'type': 'integer'}}
LangChain ile LLM Uygulamaları Geliştirme

Özel aracı entegre etmek

from langgraph.prebuilt import create_react_agent

llm = ChatOpenAI(model="gpt-4o-mini", api_key=openai_api_key, temperature=0)
agent = create_react_agent(llm, [financial_report])

messages = agent.invoke({"messages": [("human", "TechStack generated made $10 million with $8 million of costs. Generate a financial report.")]}) print(messages)
LangChain ile LLM Uygulamaları Geliştirme

Özel aracı entegre etmek

{'messages': [
    HumanMessage(content='TechStack generated made $10 million dollars with $8 million of...', ...),
    AIMessage(content='', ..., tool_calls=[{'name': 'financial_report',
                                            'args': {'company_name': 'TechStack',
                                                     'revenue': 10000000, 'expenses': 8000000}, ...),
    ToolMessage(content='Financial Report for TechStack:\nRevenue: $10000000\nExpenses...', ...),
    AIMessage(content='Here is the financial report for TechStack...', ...)
]}
LangChain ile LLM Uygulamaları Geliştirme

Araç çıktıları

print(messages['messages'][-1].content)
İşte TechStack için finansal rapor:
- Gelir: $10,000,000
- Gider: $8,000,000
- Net Gelir: $2,000,000
TechStack için Finansal Rapor:
Gelir: $10000000
Gider: $8000000
Net Gelir: $2000000
LangChain ile LLM Uygulamaları Geliştirme

Ayo berlatih!

LangChain ile LLM Uygulamaları Geliştirme

Preparing Video For Download...