### Install langchain-brainiall Source: https://github.com/fasuizu-br/langchain-brainiall/blob/main/README.md Install the langchain-brainiall package using pip. This is the first step to using the Brainiall LLM Gateway with LangChain. ```bash pip install langchain-brainiall ``` -------------------------------- ### Streaming Responses with ChatBrainiall Source: https://context7.com/fasuizu-br/langchain-brainiall/llms.txt Illustrates how to stream responses token-by-token using ChatBrainiall for real-time output. Includes both synchronous and asynchronous streaming examples. ```python from langchain_brainiall import ChatBrainiall llm = ChatBrainiall(model="claude-sonnet-4-6") # Stream response chunks for chunk in llm.stream("Tell me a joke about programming"): print(chunk.content, end="", flush=True) # Async streaming import asyncio async def stream_async(): llm = ChatBrainiall(model="claude-haiku-4-5") async for chunk in llm.astream("Write a haiku about coding"): print(chunk.content, end="", flush=True) asyncio.run(stream_async()) ``` -------------------------------- ### Integrate ChatBrainiall into LangChain Applications Source: https://context7.com/fasuizu-br/langchain-brainiall/llms.txt This example shows how to replace a standard ChatOpenAI instantiation with ChatBrainiall to gain access to a wider range of models. It emphasizes the seamless integration with existing LangChain components. ```python # Assuming you have an existing LangChain setup using ChatOpenAI # from langchain_openai import ChatOpenAI # Replace with ChatBrainiall from brainium import ChatBrainiall # Example: Initialize with a specific model (e.g., Claude Opus) llm = ChatBrainiall(model="claude-opus-4-6", api_key="YOUR_BRAINIUM_API_KEY") # Now you can use 'llm' with LangChain components, chains, and agents # For example: # response = llm.invoke("Hello, world!") # print(response.content) ``` -------------------------------- ### Multi-Model Chains with ChatBrainiall Source: https://github.com/fasuizu-br/langchain-brainiall/blob/main/README.md Construct chains using different ChatBrainiall models for various steps, optimizing for cost and performance. This example uses a fast model for drafting and a powerful model for refinement. ```python from langchain_brainiall import ChatBrainiall fast = ChatBrainiall(model="nova-micro", temperature=0.7) smart = ChatBrainiall(model="claude-opus-4-6", temperature=0) # Draft with a fast, cheap model draft = fast.invoke("Write a short product description for wireless earbuds") # Refine with a powerful model final = smart.invoke(f"Improve this product description:\n{draft.content}") print(final.content) ``` -------------------------------- ### Build Stateful Agents with ChatBrainiall and LangGraph Source: https://context7.com/fasuizu-br/langchain-brainiall/llms.txt Illustrates integrating ChatBrainiall with LangGraph's `create_react_agent` to build stateful, multi-step AI agents. This example defines tools for flight searching, hotel booking, and weather retrieval, then uses them within a ReAct agent to plan a trip. ```python from langchain_brainiall import ChatBrainiall from langgraph.prebuilt import create_react_agent from langchain_core.tools import tool from pydantic import BaseModel, Field # Define tools for the agent @tool def search_flights(destination: str, date: str) -> str: """Search for available flights to a destination.""" return f"Found 5 flights to {destination} on {date}. Prices range from $200-$500." @tool def book_hotel(city: str, checkin: str, checkout: str) -> str: """Book a hotel in a city for given dates.""" return f"Hotel booked in {city} from {checkin} to {checkout}. Confirmation #12345." @tool def get_weather(location: str) -> str: """Get weather forecast for a location.""" return f"Weather in {location}: Sunny, 25°C, low humidity." # Create the LLM llm = ChatBrainiall(model="claude-sonnet-4-6", temperature=0) # Create a ReAct agent with tools tools = [search_flights, book_hotel, get_weather] agent = create_react_agent(llm, tools) # Run the agent result = agent.invoke({ "messages": [("human", "Help me plan a trip to Japan next month. Check flights, weather, and book a hotel.")] }) # Print the agent's responses for message in result["messages"]: print(f"{message.type}: {message.content}") ``` -------------------------------- ### ChatBrainiall Tool Calling Source: https://github.com/fasuizu-br/langchain-brainiall/blob/main/README.md Enable ChatBrainiall LLM to call external tools by binding Pydantic models. This example demonstrates how to define a tool for getting weather information. ```python from pydantic import BaseModel, Field from langchain_brainiall import ChatBrainiall class GetWeather(BaseModel): """Get current weather for a location.""" location: str = Field(description="City name") llm = ChatBrainiall(model="claude-sonnet-4-6") llm_with_tools = llm.bind_tools([GetWeather]) response = llm_with_tools.invoke("What's the weather in Tokyo?") print(response.tool_calls) ``` -------------------------------- ### LangGraph Agents with ChatBrainiall Source: https://github.com/fasuizu-br/langchain-brainiall/blob/main/README.md Integrate ChatBrainiall LLM with LangGraph agents for complex task execution. This example shows setting up a ReAct agent with defined tools. ```python from langchain_brainiall import ChatBrainiall from langgraph.prebuilt import create_react_agent llm = ChatBrainiall(model="claude-sonnet-4-6") # Define your tools tools = [...] agent = create_react_agent(llm, tools) result = agent.invoke({"messages": [("human", "Help me plan a trip to Japan")]}) ``` -------------------------------- ### Structured Output Extraction with ChatBrainiall Source: https://context7.com/fasuizu-br/langchain-brainiall/llms.txt Shows how to extract structured data from LLM responses using Pydantic models for type-safe parsing. This example is incomplete as the Pydantic model definition and LLM invocation are missing. ```python from pydantic import BaseModel, Field from langchain_brainiall import ChatBrainiall ``` -------------------------------- ### Access ChatBrainiall Model Information Programmatically Source: https://context7.com/fasuizu-br/langchain-brainiall/llms.txt Provides methods to programmatically access information about available ChatBrainiall models. Users can retrieve a list of all models, get detailed information for a specific model (like context window and max output tokens), and check if a model exists. ```python from langchain_brainiall import ChatBrainiall # Get list of all available models models = ChatBrainiall.get_available_models() print(f"Available models: {len(models)}") for model in models[:5]: print(f" - {model}") # Get specific model information model_info = ChatBrainiall.get_model_info("claude-opus-4-6") print(f"\nclaude-opus-4-6:") print(f" Context window: {model_info['context']:,} tokens") print(f" Max output: {model_info['max_output']:,} tokens") # Get info for different models for model_name in ["deepseek-r1", "llama-4-maverick-17b", "minimax-m2"]: info = ChatBrainiall.get_model_info(model_name) if info: print(f"\n{model_name}:") print(f" Context: {info['context']:,}, Max output: {info['max_output']:,}") # Check if a model exists unknown = ChatBrainiall.get_model_info("nonexistent-model") print(f"\nUnknown model info: {unknown}") # Output: None ``` -------------------------------- ### Initialize Other Models (Minimax, Nemotron, Kimi) with ChatBrainiall Source: https://context7.com/fasuizu-br/langchain-brainiall/llms.txt Demonstrates initializing other supported models like Minimax, Nemotron (NVIDIA), and Kimi (Moonshot) using ChatBrainiall, specifying their respective model identifiers and context/output details. ```python from brainium import ChatBrainiall minimax = ChatBrainiall(model="minimax-m2") # 1M context, 128K output nemotron = ChatBrainiall(model="nemotron-ultra-253b") # NVIDIA kimi = ChatBrainiall(model="kimi-k2.5") # Moonshot ``` -------------------------------- ### Initialize Qwen Models with ChatBrainiall Source: https://context7.com/fasuizu-br/langchain-brainiall/llms.txt This snippet illustrates initializing Qwen models using ChatBrainiall, specifying models with a 128K context window. ```python from brainium import ChatBrainiall qwen_235b = ChatBrainiall(model="qwen-3-235b") # 128K context qwen_80b = ChatBrainiall(model="qwen-3-80b") # 128K context ``` -------------------------------- ### Initialize Meta Llama Models with ChatBrainiall Source: https://context7.com/fasuizu-br/langchain-brainiall/llms.txt Demonstrates initializing Meta's Llama models with ChatBrainiall, showcasing different sizes and context window capabilities, including models with up to 1M context. ```python from brainium import ChatBrainiall llama_70b = ChatBrainiall(model="llama-3.3-70b") # 128K context llama_scout = ChatBrainiall(model="llama-4-scout-17b") # 1M context llama_maverick = ChatBrainiall(model="llama-4-maverick-17b") # 1M context ``` -------------------------------- ### Initialize and Use BrainiallEmbeddings Source: https://context7.com/fasuizu-br/langchain-brainiall/llms.txt Demonstrates how to initialize BrainiallEmbeddings, embed a single query, and embed multiple documents. It shows how to access the dimensions and values of the generated embeddings. ```python from langchain_brainiall import BrainiallEmbeddings # Initialize embeddings (defaults to bge-m3 model) embeddings = BrainiallEmbeddings( model="bge-m3", api_key="your-api-key", # or set BRAINIALL_API_KEY env var ) # Embed a single query query_vector = embeddings.embed_query("What is machine learning?") print(f"Dimensions: {len(query_vector)}") # Output: Dimensions: 1024 print(f"First 5 values: {query_vector[:5]}") # Embed multiple documents documents = [ "Machine learning is a subset of artificial intelligence.", "Deep learning uses neural networks with many layers.", "Natural language processing enables computers to understand text.", ] doc_vectors = embeddings.embed_documents(documents) print(f"Number of vectors: {len(doc_vectors)}") # Output: 3 print(f"Each vector dimension: {len(doc_vectors[0])}") # Output: 1024 ``` -------------------------------- ### Initialize Mistral Models with ChatBrainiall Source: https://context7.com/fasuizu-br/langchain-brainiall/llms.txt Shows how to initialize Mistral models, including 'mistral-large-3' and 'mistral-small-3', both with a 128K context window, using the ChatBrainiall class. ```python from brainium import ChatBrainiall mistral_large = ChatBrainiall(model="mistral-large-3") # 128K context mistral_small = ChatBrainiall(model="mistral-small-3") # 128K context ``` -------------------------------- ### Initialize DeepSeek Models with ChatBrainiall Source: https://context7.com/fasuizu-br/langchain-brainiall/llms.txt This code shows the initialization of DeepSeek models using ChatBrainiall. It includes a reasoning model ('deepseek-r1') and a general model ('deepseek-v3'), both supporting a 128K context window. ```python from brainium import ChatBrainiall deepseek_r1 = ChatBrainiall(model="deepseek-r1") # 128K context, reasoning model deepseek_v3 = ChatBrainiall(model="deepseek-v3") # 128K context ``` -------------------------------- ### Initialize Amazon Nova Models with ChatBrainiall Source: https://context7.com/fasuizu-br/langchain-brainiall/llms.txt This code initializes various Amazon Nova models with ChatBrainiall, highlighting their context window sizes and characteristics like cost-effectiveness and speed. ```python from brainium import ChatBrainiall nova_pro = ChatBrainiall(model="nova-pro") # 300K context, cost-effective nova_lite = ChatBrainiall(model="nova-lite") # 300K context nova_micro = ChatBrainiall(model="nova-micro") # 128K context, fastest ``` -------------------------------- ### Configure ChatBrainiall and BrainiallEmbeddings with Environment Variables Source: https://context7.com/fasuizu-br/langchain-brainiall/llms.txt Explains how to configure ChatBrainiall and BrainiallEmbeddings using environment variables like BRAINIALL_API_KEY and BRAINIALL_API_BASE. It shows how these variables are automatically read and how explicit parameters can override them. ```python import os from langchain_brainiall import ChatBrainiall, BrainiallEmbeddings # Set environment variables (typically done outside Python) os.environ["BRAINIALL_API_KEY"] = "your-api-key" os.environ["BRAINIALL_API_BASE"] = "https://custom-endpoint.example.com/v1" # Optional override # Both classes automatically read from environment llm = ChatBrainiall(model="claude-sonnet-4-6") # Uses BRAINIALL_API_KEY embeddings = BrainiallEmbeddings() # Uses BRAINIALL_API_KEY # Explicit values override environment variables llm_custom = ChatBrainiall( model="deepseek-r1", api_key="different-api-key", # Overrides BRAINIALL_API_KEY base_url="https://another-endpoint.com/v1", # Overrides BRAINIALL_API_BASE ) # Check the configured base URL print(f"Default base URL: {llm.openai_api_base}") print(f"Custom base URL: {llm_custom.openai_api_base}") ``` -------------------------------- ### Tool Calling with ChatBrainiall Source: https://context7.com/fasuizu-br/langchain-brainiall/llms.txt Demonstrates how to bind Pydantic models as tools to ChatBrainiall for function calling. It shows invoking the model with a query that triggers tool use and accessing the tool call details. ```python from pydantic import BaseModel, Field from langchain_brainiall import ChatBrainiall # Define tools as Pydantic models class GetWeather(BaseModel): """Get current weather for a location.""" location: str = Field(description="City name, e.g., 'Tokyo'") unit: str = Field(default="celsius", description="Temperature unit") class SearchDatabase(BaseModel): """Search the product database.""" query: str = Field(description="Search query string") limit: int = Field(default=10, description="Maximum results to return") llm = ChatBrainiall(model="claude-sonnet-4-6", temperature=0) # Bind tools to the model llm_with_tools = llm.bind_tools([GetWeather, SearchDatabase]) # Invoke with a query that should trigger tool use response = llm_with_tools.invoke("What's the weather in Tokyo?") # Access tool calls from the response print(response.tool_calls) # Output: [{'name': 'GetWeather', 'args': {'location': 'Tokyo', 'unit': 'celsius'}, 'id': '...'}] # Check the tool name and arguments if response.tool_calls: tool_call = response.tool_calls[0] print(f"Tool: {tool_call['name']}") print(f"Args: {tool_call['args']}") ``` -------------------------------- ### Define Structured Output Schemas with ChatBrainiall Source: https://context7.com/fasuizu-br/langchain-brainiall/llms.txt Demonstrates how to define Pydantic models for structured output from ChatBrainiall. This allows the LLM to return data in a predictable format, such as movie reviews or mathematical answers, with specified fields and descriptions. ```python from langchain_brainiall import ChatBrainiall from pydantic import BaseModel, Field class MovieReview(BaseModel): title: str = Field(description="Movie title") rating: float = Field(description="Rating from 0-10") summary: str = Field(description="Brief review summary") recommended: bool = Field(description="Whether to recommend the movie") class MathAnswer(BaseModel): result: int = Field(description="The numeric result") explanation: str = Field(description="Step-by-step explanation") llm = ChatBrainiall(model="claude-sonnet-4-6", temperature=0) # Create structured output chain for movie review structured_review = llm.with_structured_output(MovieReview) review = structured_review.invoke("Review the movie Inception") print(f"{review.title}: {review.rating}/10") print(f"Summary: {review.summary}") print(f"Recommended: {review.recommended}") # Math problem with structured output structured_math = llm.with_structured_output(MathAnswer) answer = structured_math.invoke("What is 15 + 27?") print(f"Result: {answer.result}") # Output: Result: 42 print(f"Explanation: {answer.explanation}") ``` -------------------------------- ### BrainiallEmbeddings with Different Models Source: https://context7.com/fasuizu-br/langchain-brainiall/llms.txt Shows how to initialize BrainiallEmbeddings with various available models like bge-m3, cohere-embed-v3, titan-embed-v2, and bge-large-en-v1.5. It also demonstrates how to retrieve the list of available models and compare embeddings from different models. ```python from langchain_brainiall import BrainiallEmbeddings # BGE-M3: Multilingual, high-quality embeddings bge_embeddings = BrainiallEmbeddings(model="bge-m3") # 1024 dims, 8192 max tokens # Cohere Embed V3: Strong semantic understanding cohere_embeddings = BrainiallEmbeddings(model="cohere-embed-v3") # 1024 dims, 512 max tokens # Titan Embed V2: AWS-native embeddings titan_embeddings = BrainiallEmbeddings(model="titan-embed-v2") # 1024 dims, 8192 max tokens # BGE Large English: English-optimized bge_en_embeddings = BrainiallEmbeddings(model="bge-large-en-v1.5") # 1024 dims, 512 max tokens # Get available embedding models available = BrainiallEmbeddings.get_available_models() print(f"Available embedding models: {available}") # Output: ['bge-large-en-v1.5', 'bge-m3', 'cohere-embed-v3', 'titan-embed-v2'] # Compare embeddings from different models text = "Artificial intelligence is transforming industries." for model_name in ["bge-m3", "cohere-embed-v3"]: emb = BrainiallEmbeddings(model=model_name) vector = emb.embed_query(text) print(f"{model_name}: {len(vector)} dimensions") ``` -------------------------------- ### Initialize ChatBrainiall LLM Source: https://github.com/fasuizu-br/langchain-brainiall/blob/main/README.md Initialize the ChatBrainiall LLM for making direct text generation calls. Requires an API key, which can be provided directly or set as an environment variable. ```python from langchain_brainiall import ChatBrainiall llm = ChatBrainiall( model="claude-sonnet-4-6", api_key="your-api-key", # or set BRAINIALL_API_KEY env var ) response = llm.invoke("Explain quantum computing in one sentence.") print(response.content) ``` -------------------------------- ### Initialize Anthropic Claude Models with ChatBrainiall Source: https://context7.com/fasuizu-br/langchain-brainiall/llms.txt This snippet demonstrates how to initialize different Anthropic Claude models using the ChatBrainiall class. It highlights variations in context window and output token limits for models like Opus, Sonnet, and Haiku. ```python from brainium import ChatBrainiall claude_opus = ChatBrainiall(model="claude-opus-4-6") # 200K context, 64K output claude_opus_1m = ChatBrainiall(model="claude-opus-4-6-1m") # 1M context, 64K output claude_sonnet = ChatBrainiall(model="claude-sonnet-4-6") # 200K context, 64K output claude_haiku = ChatBrainiall(model="claude-haiku-4-5") # 200K context, 16K output ``` -------------------------------- ### Basic Chat Completion with ChatBrainiall Source: https://context7.com/fasuizu-br/langchain-brainiall/llms.txt Demonstrates basic usage of the ChatBrainiall class for generating text responses. It shows initialization with model parameters and API key, and invoking the model with both string and message list prompts. ```python from langchain_brainiall import ChatBrainiall # Initialize with API key (or set BRAINIALL_API_KEY env var)llm = ChatBrainiall( model="claude-sonnet-4-6", api_key="your-api-key", temperature=0, max_tokens=1024, ) # Simple invocation with a string prompt response = llm.invoke("Explain quantum computing in one sentence.") print(response.content) # Invocation with system and human messages messages = [ ("system", "You are a helpful assistant. Be concise."), ("human", "What is the capital of France?"), ] response = llm.invoke(messages) print(response.content) # Output: Paris is the capital of France. ``` -------------------------------- ### Initialize BrainiallEmbeddings Source: https://github.com/fasuizu-br/langchain-brainiall/blob/main/README.md Initialize BrainiallEmbeddings for generating vector embeddings. This requires specifying a model and optionally an API key. ```python from langchain_brainiall import BrainiallEmbeddings embeddings = BrainiallEmbeddings( model="bge-m3", api_key="your-api-key", ) vector = embeddings.embed_query("What is machine learning?") print(f"Dimensions: {len(vector)}") ``` -------------------------------- ### BrainiallEmbeddings with Vector Stores Source: https://context7.com/fasuizu-br/langchain-brainiall/llms.txt Illustrates how to use BrainiallEmbeddings with LangChain vector stores. It covers embedding documents, embedding a query, and calculating cosine similarity to find the most relevant documents. ```python from langchain_brainiall import BrainiallEmbeddings from langchain_core.documents import Document import numpy as np # Initialize embeddings embeddings = BrainiallEmbeddings(model="bge-m3") # Example documents documents = [ Document(page_content="Python is a versatile programming language.", metadata={"topic": "programming"}), Document(page_content="Machine learning models require training data.", metadata={"topic": "ml"}), Document(page_content="Vector databases store embeddings efficiently.", metadata={"topic": "databases"}), Document(page_content="LangChain simplifies building LLM applications.", metadata={"topic": "frameworks"}), ] # Embed documents for vector store ingestion texts = [doc.page_content for doc in documents] vectors = embeddings.embed_documents(texts) # Embed a query for similarity search query = "How do I build AI applications?" query_vector = embeddings.embed_query(query) # Calculate similarity (cosine similarity example) def cosine_similarity(a, b): return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b)) # Find most similar documents similarities = [cosine_similarity(query_vector, vec) for vec in vectors] for doc, sim in sorted(zip(documents, similarities), key=lambda x: x[1], reverse=True): print(f"Score: {sim:.4f} - {doc.page_content[:50]}...") ``` -------------------------------- ### Available Chat Models Reference Source: https://context7.com/fasuizu-br/langchain-brainiall/llms.txt Provides a reference for accessing over 113 chat models from major AI providers through the ChatBrainiall class. ```python from langchain_brainiall import ChatBrainiall ``` -------------------------------- ### ChatBrainiall Structured Output Source: https://github.com/fasuizu-br/langchain-brainiall/blob/main/README.md Generate structured output from the ChatBrainiall LLM using Pydantic models. This ensures the LLM's response conforms to a predefined schema. ```python from pydantic import BaseModel from langchain_brainiall import ChatBrainiall class MovieReview(BaseModel): title: str rating: float summary: str llm = ChatBrainiall(model="claude-sonnet-4-6") structured = llm.with_structured_output(MovieReview) review = structured.invoke("Review the movie Inception") print(f"{review.title}: {review.rating}/10 - {review.summary}") ``` -------------------------------- ### Asynchronous Operations with ChatBrainiall Source: https://context7.com/fasuizu-br/langchain-brainiall/llms.txt Shows how to perform asynchronous operations like `ainvoke` and `abatch` with ChatBrainiall, enabling non-blocking calls suitable for async applications. ```python import asyncio from langchain_brainiall import ChatBrainiall async def main(): llm = ChatBrainiall(model="claude-haiku-4-5", temperature=0) # Async invoke response = await llm.ainvoke("Hello!") print(response.content) # Batch async invocations prompts = ["What is 2+2?", "What is 3+3?", "What is 4+4?"] responses = await llm.abatch(prompts) for prompt, resp in zip(prompts, responses): print(f"{prompt} -> {resp.content}") asyncio.run(main()) ``` -------------------------------- ### Stream ChatBrainiall Responses Source: https://github.com/fasuizu-br/langchain-brainiall/blob/main/README.md Implement streaming responses from the ChatBrainiall LLM. This allows for real-time display of generated text as it becomes available. ```python from langchain_brainiall import ChatBrainiall llm = ChatBrainiall(model="claude-sonnet-4-6") for chunk in llm.stream("Tell me a joke about programming"): print(chunk.content, end="", flush=True) ``` -------------------------------- ### Basic Usage of BrainiallEmbeddings Source: https://context7.com/fasuizu-br/langchain-brainiall/llms.txt Introduces the BrainiallEmbeddings class, which provides access to various embedding models. This class is used for generating vector representations of text, a fundamental step for many NLP tasks like semantic search and clustering. ```python from langchain_brainiall import BrainiallEmbeddings # Example usage (assuming BrainiallEmbeddings is instantiated and used elsewhere) ``` -------------------------------- ### Async ChatBrainiall Invocation Source: https://github.com/fasuizu-br/langchain-brainiall/blob/main/README.md Perform asynchronous invocations with the ChatBrainiall LLM. This is useful for non-blocking operations in applications. ```python import asyncio from langchain_brainiall import ChatBrainiall async def main(): llm = ChatBrainiall(model="claude-haiku-4-5") response = await llm.ainvoke("Hello!") print(response.content) asyncio.run(main()) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.