LangDB integrates seamlessly with popular libraries like LangChain, providing tracing support to capture detailed logs for workflows. Below is a complete example of using LangChain with LangDB.
Pre-requisites
-
Tavily API token
-
OpenAI API token
-
Python v3.11
-
Pip packages: langchain (at least v0.1.0), openai, wikipedia, langchain-community, tavily-python, langchainhub, langchain-openai, python-dotenv
! pip install langchain==0.1.0 wikipedia==1.4.0 langchain-community=0.0.10 tavily-python==0.3.0 langchainhub==0.1.14 langchain-openai==0.0.2 openai==1.7.0 python-dotenv==1.0.0
Code
from langchain import hub
from langchain.agents import (
AgentExecutor,
create_react_agent,
create_openai_functions_agent,
create_tool_calling_agent
)
from langchain_core.tools import Tool
from langchain_openai import ChatOpenAI
import requests
import json
import os
from langchain_community.agent_toolkits.load_tools import load_tools
from langchain_community.tools.tavily_search.tool import TavilySearchResults
from langchain_community.utilities.tavily_search import TavilySearchAPIWrapper
import uuid
api_base = "https://api.us-east-1.langdb.ai" # LangDB API base URL
api_key = get_access_token(client_id, client_secret, api_base)
pre_defined_run_id = uuid.uuid4()
default_headers = {
"x-project-id": "xxxxx", ### LangDB Project ID
"x-thread-id": str(pre_defined_run_id),
}
os.environ['OPENAI_API_KEY'] = 'LANGDB_API_KEY'
os.environ['TAVILY_API_KEY'] = 'tvly-xxxx'
def get_function_tools():
search = TavilySearchAPIWrapper()
tavily_tool = TavilySearchResults(api_wrapper=search)
tools = [
tavily_tool
]
tools.extend(load_tools(['wikipedia']))
return tools
def init_action():
llm = ChatOpenAI(model_name='gpt-4o' , temperature=0.3, openai_api_base=api_base, default_headers=default_headers,disable_streaming=True )
prompt = hub.pull("hwchase17/openai-functions-agent")
tools = get_function_tools()
agent = create_tool_calling_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
agent_executor.invoke({"input": "Who is the owner of Tesla company? Let me know details about owner."})
init_action()