How Langchain::Assistant works
mainThe Langchain::Assistant class is a high-level abstraction that combines Large Language Models (LLMs), tools, and conversation management. It is designed to handle complex, multi-turn conversations, manage conversation threads, and execute tools (either automatically or manually) to provide coherent responses based on context.
An assistant can be initialized with an LLM, a set of tools, and system instructions. It supports features like automatic tool execution, streaming responses (where supported by the LLM), and image input.
llm = Langchain::LLM::OpenAI.new(api_key: ENV["OPENAI_API_KEY"])
assistant = Langchain::Assistant.new(
llm: llm,
instructions: "You're a helpful AI assistant",
tools: [Langchain::Tool::NewsRetriever.new(api_key: ENV["NEWS_API_KEY"])]
)
# Run the assistant with automatic tool execution
assistant.add_message_and_run!(content: "What's the latest news about AI?")
assistant.run(auto_tool_execution: true)