### Install langchain-sambanova Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/usage_guide.ipynb Install the necessary package using pip. The `-qU` flags ensure quiet installation and upgrade. ```python # Install the package # %pip install -qU langchain-sambanova ``` -------------------------------- ### Run Async Chat Example Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/usage_guide.ipynb Execute an asynchronous chat example. Ensure the async_chat_example function is defined and await its completion. ```python await async_chat_example() ``` -------------------------------- ### Install langchain-sambanova Package Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/provider.ipynb Install the necessary package for SambaNova integration with LangChain. ```bash pip install langchain-sambanova ``` -------------------------------- ### Install langchain-sambanova (Jupyter) Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/text_embedding_sambanova.ipynb Install the LangChain SambaNova integration package using pip in a Jupyter environment. ```python %pip install -qU langchain-sambanova ``` -------------------------------- ### Create and Use a Chat Chain with Prompt Template Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/chat_sambanova.ipynb Create a chat chain using a ChatPromptTemplate and the ChatSambaNova model. This example demonstrates streaming the response. ```python from langchain_core.prompts import ChatPromptTemplate system = "You are a helpful assistant with pirate accent." human = "I want to learn more about this animal: {animal}" prompt = ChatPromptTemplate.from_messages([("system", system), ("human", human)]) chain = prompt | llm for chunk in chain.stream({"animal": "owl"}): print(chunk.content, end="", flush=True) ``` -------------------------------- ### Environment Setup for SambaNova API Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/usage_guide.ipynb Load SambaNova API credentials from a .env file or set them directly as environment variables. Optionally, configure the SambaStack base URL. ```python import os from dotenv import load_dotenv # Load environment variables from .env file load_dotenv("../.env") # Or set directly: # os.environ["SAMBANOVA_API_KEY"] = "your-api-key-here" # Optional: For SambaStack deployments, set the base URL # os.environ["SAMBANOVA_API_BASE"] = "your-sambastack-url" ``` -------------------------------- ### Asynchronous Streaming with Prompt Template Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/chat_sambanova.ipynb Demonstrates asynchronous streaming of responses from the ChatSambaNova model using a prompt template. This example limits the explanation length. ```python from langchain_core.prompts import ChatPromptTemplate prompt = ChatPromptTemplate.from_messages( [ ( "human", "in less than {num_words} words explain me {topic} ", ) ] ) chain = prompt | llm async for chunk in chain.astream({"num_words": 30, "topic": "quantum computers"}): print(chunk.content, end="", flush=True) ``` -------------------------------- ### RAG Integration with Chroma and SambaNova Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/usage_guide.ipynb Sets up a vector store using Chroma and SambaNova embeddings for RAG. Requires `chromadb` to be installed. ```python # Install chroma if needed # %pip install -q chromadb ``` ```python from langchain_community.vectorstores import Chroma from langchain_core.documents import Document # Create sample documents documents = [ Document( page_content="Python is a versatile programming language used in web development, data science, and automation.", metadata={"source": "python_intro", "topic": "programming"}, ), Document( page_content="Machine learning models can be trained on large datasets to make predictions and decisions.", metadata={"source": "ml_basics", "topic": "ai"}, ), Document( page_content="Vector databases store embeddings for efficient similarity search in RAG applications.", metadata={"source": "rag_guide", "topic": "database"}, ), Document( page_content="LangChain provides tools for building applications with language models and embeddings.", metadata={"source": "langchain_intro", "topic": "framework"}, ), ] # Create embeddings and vector store embeddings = SambaNovaEmbeddings(model="E5-Mistral-7B-Instruct") vectorstore = Chroma.from_documents(documents, embeddings) print("Vector store created with", len(documents), "documents") ``` -------------------------------- ### Asynchronous Invocation with Prompt Template Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/chat_sambanova.ipynb Use an asynchronous invocation with a prompt template to get a response from the ChatSambaNova model. This example queries for the capital of a country. ```python from langchain_core.prompts import ChatPromptTemplate prompt = ChatPromptTemplate.from_messages( [ ( "human", "what is the capital of {country}?", ) ] ) chain = prompt | llm await chain.ainvoke({"country": "France"}) ``` -------------------------------- ### Get SambaNova API Key from User Input Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/text_embedding_sambanova.ipynb Prompt the user for their SambaNova API key if it's not already set in the environment. ```python import getpass import os if not os.getenv("SAMBANOVA_API_KEY"): os.environ["SAMBANOVA_API_KEY"] = getpass.getpass("Enter your SambaNova API key: ") ``` -------------------------------- ### Manage Multi-Turn Conversations with ChatSambaNova Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/usage_guide.ipynb Maintain conversation history for multi-turn interactions. This example demonstrates starting and continuing a conversation with a coding assistant. ```python llm = ChatSambaNova(model="Meta-Llama-3.3-70B-Instruct", temperature=0.7) # Start conversation conversation = [ SystemMessage(content="You are a helpful coding assistant."), HumanMessage(content="What is a Python decorator?"), ] response = llm.invoke(conversation) print("Assistant:", response.content) conversation.append(response) # Continue conversation conversation.append(HumanMessage(content="Can you show me a simple example?")) response = llm.invoke(conversation) print("\nAssistant:", response.content) conversation.append(response) ``` -------------------------------- ### Basic Python Decorator Example Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/usage_guide.ipynb A simple decorator that adds behavior before and after a function call. The `@` syntax is syntactic sugar for function wrapping. ```python def my_decorator(func): def wrapper(): print("Something is happening before the function is called.") func() print("Something is happening after the function is called.") return wrapper @my_decorator def say_hello(): print("Hello!") say_hello() ``` -------------------------------- ### Configure Stop Sequences for ChatSambaNova Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/usage_guide.ipynb Set stop sequences to control when the model terminates generation. This example stops at a double newline or the phrase 'Conclusion:'. ```python llm = ChatSambaNova( model="Meta-Llama-3.3-70B-Instruct", max_tokens=512, stop=["\n\n", "Conclusion:"], # Stop at double newline or "Conclusion:" ) response = llm.invoke("Write a brief history of the internet. Start with ARPANET.") print(response.content) print("\n[Generation stopped at stop sequence]") ``` -------------------------------- ### JSON Mode for Unvalidated JSON Output Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/usage_guide.ipynb Force the model to return valid JSON without strict schema validation. This example uses JSON mode to get an AI joke. ```python # Simple JSON mode structured_llm = llm.with_structured_output(joke_schema, method="json_mode") joke = structured_llm.invoke( "Tell me a joke about artificial intelligence. Return as JSON with setup and punchline." ) print(f"Result type: {type(joke)}") print(f"Setup: {joke['setup']}") print(f"Punchline: {joke['punchline']}") ``` -------------------------------- ### Pydantic Model for Structured Output - Joke Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/usage_guide.ipynb Demonstrates using `with_structured_output` with a Pydantic model to get structured JSON responses from the LLM. This is ideal for predictable data formats. Ensure the Pydantic model is correctly defined with descriptions for fields. ```python from pydantic import BaseModel, Field class Joke(BaseModel): """A joke with setup and punchline.""" setup: str = Field(description="The setup of the joke") punchline: str = Field(description="The punchline of the joke") llm = ChatSambaNova(model="Meta-Llama-3.3-70B-Instruct", temperature=0.7) structured_llm = llm.with_structured_output(Joke) joke = structured_llm.invoke("Tell me a joke about programming") print(f"Setup: {joke.setup}") print(f"Punchline: {joke.punchline}") ``` -------------------------------- ### Direct JSON Binding Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/usage_guide.ipynb Use direct JSON binding by setting the response_format to json_object. This example lists programming languages and their use cases. ```python # Alternative: Direct JSON binding llm_json = llm.bind(response_format={"type": "json_object"}) response = llm_json.invoke( "List 3 programming languages with their primary use cases. " "Return as JSON with 'languages' array containing objects with 'name' and 'use_case' fields." ) import json result = json.loads(response.content) print("Languages:") for lang in result["languages"]: print(f" - {lang['name']}: {lang['use_case']}") ``` -------------------------------- ### JSON Schema for Structured Output Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/usage_guide.ipynb Define schemas using raw JSON Schema for maximum flexibility. This example uses a JSON schema to extract a programming joke. ```python joke_schema = { "title": "Joke", "description": "A joke with setup and punchline", "type": "object", "properties": { "setup": {"type": "string", "description": "The setup of the joke"}, "punchline": {"type": "string", "description": "The punchline of the joke"}, "category": { "type": "string", "description": "The category of the joke", "enum": ["programming", "science", "general", "wordplay"], }, }, "required": ["setup", "punchline"], } structured_llm = llm.with_structured_output(joke_schema, method="json_schema") joke = structured_llm.invoke("Tell me a programming joke") print(f"Setup: {joke['setup']}") print(f"Punchline: {joke['punchline']}") print(f"Category: {joke.get('category', 'Unknown')}") ``` -------------------------------- ### TypedDict for Structured Output Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/usage_guide.ipynb Use TypedDict for simpler schemas when Pydantic is not needed. This example defines a joke schema and extracts structured joke data. ```python from typing import Optional from typing_extensions import Annotated, TypedDict class JokeDict(TypedDict): """A joke with setup and punchline.""" setup: Annotated[str, "The setup of the joke"] punchline: Annotated[str, "The punchline of the joke"] rating: Annotated[Optional[int], "Rating from 1-10, optional"] = None structured_llm = llm.with_structured_output(JokeDict) joke = structured_llm.invoke("Tell me a joke about databases") print(f"Setup: {joke['setup']}") print(f"Punchline: {joke['punchline']}") print(f"Rating: {joke.get('rating', 'Not rated')}") ``` -------------------------------- ### Multi-Modal Image Support Initialization Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/usage_guide.ipynb Initialize a Maverick model that supports vision for analyzing images alongside text. This snippet shows the initialization of such a model. ```python import base64 import httpx image_b64 = "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/2wBDAQMDAwQDBAgEBAgQCwkLEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBD/wAARCAAHAAgDASIAAhEBAxEB/8QAFQABAQAAAAAAAAAAAAAAAAAAAAb/xAAhEAABAgQHAAAAAAAAAAAAAAATABQEFRYjBhESFyQyNP/EABUBAQEAAAAAAAAAAAAAAAAAAAME/8QAIBEAAQIFBQAAAAAAAAAAAAAAAQIRAAMEEiETMVFhof/aAAwDAQACEQMRAD8AvgYkau6Ng9zp5LT1dEPZiZv6GegjvkZkEKz0soiI0ywh7TFs+tVU26qQWDDcMOMEe57j/9k=" # Initialize Maverick model (supports vision) llm_vision = ChatSambaNova(model="Llama-4-Maverick-17B-128E-Instruct", max_tokens=512) ``` -------------------------------- ### Index and Retrieve with SambaNovaEmbeddings Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/text_embedding_sambanova.ipynb Demonstrates indexing a text document into an InMemoryVectorStore and retrieving it using SambaNovaEmbeddings. ```python # Create a vector store with a sample text from langchain_core.vectorstores import InMemoryVectorStore text = "LangChain is the framework for building context-aware reasoning applications" vectorstore = InMemoryVectorStore.from_texts( [text], embedding=embeddings, ) # Use the vectorstore as a retriever retriever = vectorstore.as_retriever() # Retrieve the most similar text retrieved_documents = retriever.invoke("What is LangChain?") # show the retrieved document's content retrieved_documents[0].page_content ``` -------------------------------- ### Initialize and Use ChatSambaNova Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/provider.ipynb Initialize the ChatSambaNova model and invoke it for conversational AI tasks. Ensure the model name and temperature are set appropriately. ```python from langchain_sambanova import ChatSambaNova llm = ChatSambaNova(model="Llama-4-Maverick-17B-128E-Instruct", temperature=0.7) llm.invoke("Tell me a joke about artificial intelligence.") ``` -------------------------------- ### Instantiate ChatSambaNova Model Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/chat_sambanova.ipynb Instantiate the ChatSambaNova model with specified parameters like model name, max tokens, temperature, and top_p. ```python from langchain_sambanova import ChatSambaNova llm = ChatSambaNova( model="Llama-4-Maverick-17B-128E-Instruct", max_tokens=1024, temperature=0.7, top_p=0.01, ) ``` -------------------------------- ### Synchronous Batching with ChatSambaNova Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/usage_guide.ipynb Demonstrates how to perform synchronous batch requests to a ChatSambaNova model. Ensure the ChatSambaNova model is initialized before use. ```python llm = ChatSambaNova(model="Meta-Llama-3.3-70B-Instruct", max_tokens=100) prompts = [ "What is the capital of France?", "What is 2 + 2?", "Name a programming language.", ] responses = llm.batch(prompts) print("Batch responses:") for i, (prompt, response) in enumerate(zip(prompts, responses), 1): print(f"\n{i}. Q: {prompt}") print(f" A: {response.content}") ``` -------------------------------- ### Full RAG Pipeline with RetrievalQA Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/usage_guide.ipynb Constructs a complete RAG pipeline using `RetrievalQA` chain, a retriever from the vector store, and a ChatSambaNova LLM. Ensure `ChatSambaNova` and `RetrievalQA` are imported. ```python # Full RAG pipeline from langchain_classic.chains import RetrievalQA # Create retriever retriever = vectorstore.as_retriever(search_kwargs={"k": 2}) # Create QA chain llm = ChatSambaNova(model="Meta-Llama-3.3-70B-Instruct", temperature=0.3) qa_chain = RetrievalQA.from_chain_type( llm=llm, chain_type="stuff", retriever=retriever, return_source_documents=True ) # Ask a question question = "What is Python used for?" result = qa_chain.invoke({"query": question}) print(f"Question: {question}\n") print(f"Answer: {result['result']}\n") print("Source documents:") for doc in result["source_documents"]: print(f" - {doc.metadata['source']}: {doc.page_content[:100]}...") ``` -------------------------------- ### Instantiate and Invoke DeepSeek-V3.1 with Thinking Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/usage_guide.ipynb Instantiate the ChatSambaNova model with DeepSeek-V3.1 and enable the 'thinking' feature via model_kwargs. Invoke the model with a problem statement and print the response content. ```python llm_deepseek = ChatSambaNova( model="DeepSeek-V3.1", max_tokens=2048, model_kwargs={"extra_body": {"chat_template_kwargs": {"enable_thinking": True}}}, ) response = llm_deepseek.invoke( "If you have a 3-gallon jug and a 5-gallon jug, how can you measure exactly 4 gallons?" ) print(response.content) print( "\nNote: DeepSeek models with thinking enabled include reasoning in their response." ) ``` -------------------------------- ### Configure SambaCloud Credentials Source: https://github.com/sambanova/langchain-sambanova/blob/main/README.md Set the SAMBANOVA_API_KEY environment variable for SambaCloud users. A free API key can be obtained from the provided link. ```bash export SAMBANOVA_API_KEY="your-sambacloud-api-key-here" ``` -------------------------------- ### Batching with Structured Output using Pydantic Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/usage_guide.ipynb Demonstrates batching requests to a ChatSambaNova model configured to return structured output using Pydantic models. Requires the pydantic library and Field from pydantic. ```python from pydantic import BaseModel class LanguageInfo(BaseModel): """Information about a programming language.""" name: str = Field(description="Name of the language") primary_use: str = Field(description="Primary use case") popularity: str = Field(description="Popularity level (high/medium/low)") structured_llm = llm.with_structured_output(LanguageInfo) prompts = ["Tell me about Python", "Tell me about Go", "Tell me about Swift"] results = structured_llm.batch(prompts) print("Structured batch results:") for i, result in enumerate(results, 1): print(f"\n{i}. {result.name}") print(f" Use: {result.primary_use}") print(f" Popularity: {result.popularity}") ``` -------------------------------- ### Initialize and Use SambaNovaEmbeddings Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/provider.ipynb Initialize SambaNovaEmbeddings with a specified model and use it to generate embeddings for text. This is useful for semantic search and other NLP tasks. ```python from langchain_sambanova import SambaNovaEmbeddings embeddings = SambaNovaEmbeddings(model="E5-Mistral-7B-Instruct") embeddings.embed_query("What is the meaning of life?") ``` -------------------------------- ### Async Embeddings with SambaNova Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/usage_guide.ipynb Demonstrates asynchronous embedding of queries and documents using SambaNovaEmbeddings. Ensure the SambaNova client is configured. ```python async def async_embeddings_example(): embeddings = SambaNovaEmbeddings(model="E5-Mistral-7B-Instruct") # Async query embedding query_embedding = await embeddings.aembed_query("What is deep learning?") print(f"Query embedding dimension: {len(query_embedding)}") # Async document embeddings documents = [ "Python is a high-level programming language.", "JavaScript is primarily used for web development.", "Rust focuses on memory safety and performance.", ] doc_embeddings = await embeddings.aembed_documents(documents) print(f"Embedded {len(doc_embeddings)} documents asynchronously") await async_embeddings_example() ``` -------------------------------- ### Instantiate SambaNovaEmbeddings Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/text_embedding_sambanova.ipynb Instantiate the SambaNovaEmbeddings model with a specified model name. ```python from langchain_sambanova import SambaNovaEmbeddings embeddings = SambaNovaEmbeddings( model="E5-Mistral-7B-Instruct", ) ``` -------------------------------- ### Synchronous Chat with ChatSambaNova Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/usage_guide.ipynb Initialize the ChatSambaNova model and invoke it with a simple string prompt for a synchronous response. ```python # Initialize the chat model llm = ChatSambaNova( model="Meta-Llama-3.3-70B-Instruct", max_tokens=1024, temperature=0.7, top_p=0.9, top_k=50, ) # Simple string prompt response = llm.invoke("What is the capital of France?") print(response.content) ``` -------------------------------- ### Configure SambaCloud API Key Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/provider.ipynb Set your SambaNovaCloud API key as an environment variable for authentication. ```bash export SAMBANOVA_API_KEY="your-sambanova-cloud-api-key-here" ``` -------------------------------- ### Define and Use a Tool with Langchain and SambaNova Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/chat_sambanova.ipynb Defines a Python tool using Langchain's `@tool` decorator and demonstrates how to integrate it with a SambaNova LLM for function calling. This is useful for enabling LLMs to interact with external tools. ```python from datetime import datetime from langchain_core.messages import HumanMessage, ToolMessage from langchain_core.tools import tool @tool def get_time(kind: str = "both") -> str: """Returns current date, current time or both. Args: kind(str): date, time or both Returns: str: current date, current time or both """ if kind == "date": date = datetime.now().strftime("%m/%d/%Y") return f"Current date: {date}" if kind == "time": time = datetime.now().strftime("%H:%M:%S") return f"Current time: {time}" date = datetime.now().strftime("%m/%d/%Y") time = datetime.now().strftime("%H:%M:%S") return f"Current date: {date}, Current time: {time}" tools = [get_time] def invoke_tools(tool_calls, messages): available_functions = {tool.name: tool for tool in tools} for tool_call in tool_calls: selected_tool = available_functions[tool_call["name"]] tool_output = selected_tool.invoke(tool_call["args"]) print(f"Tool output: {tool_output}") messages.append(ToolMessage(tool_output, tool_call_id=tool_call["id"])) return messages ``` -------------------------------- ### Initialize ChatSambaNova Model Source: https://github.com/sambanova/langchain-sambanova/blob/main/README.md Instantiate the ChatSambaNova class with a specific model and temperature. This class provides access to SambaNova chat models. ```python from langchain_sambanova import ChatSambaNova llm = ChatSambaNova( model = "Llama-4-Maverick-17B-128E-Instruct", temperature = 0.7 ) llm.invoke("Tell me a joke about artificial intelligence.") ``` -------------------------------- ### Synchronous Streaming with ChatSambaNova Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/usage_guide.ipynb Demonstrates how to receive response tokens as they are generated using the `stream` method. Ensure the `streaming` parameter is set to `True` when initializing the model. ```python llm = ChatSambaNova(model="Meta-Llama-3.3-70B-Instruct", streaming=True) prompt = "Write a short story about a robot learning to paint." print("Streaming response:") for chunk in llm.stream(prompt): print(chunk.content, end="", flush=True) print("\n\nDone!") ``` -------------------------------- ### Initialize SambaNovaEmbeddings Source: https://github.com/sambanova/langchain-sambanova/blob/main/README.md Instantiate SambaNovaEmbeddings with a chosen model. This class provides access to SambaNova's embedding capabilities. ```python from langchain_sambanova import SambaNovaEmbeddings embeddings = SambaNovaEmbeddings( model="E5-Mistral-7B-Instruct" ) embeddings.embed_query("What is the meaning of life?") ``` -------------------------------- ### Import Core LangChain and SambaNova Components Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/usage_guide.ipynb Import necessary classes for chat interactions and embeddings from LangChain and the SambaNova integration. ```python from langchain_core.messages import AIMessage, HumanMessage, SystemMessage from langchain_sambanova import ChatSambaNova, SambaNovaEmbeddings ``` -------------------------------- ### Configure SambaStack API Credentials Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/provider.ipynb Set your SambaStack API base URL and API key as environment variables for authentication. ```bash export SAMBANOVA_API_BASE="your-sambastack-api-base-url-here" export SAMBANOVA_API_KEY="your-sambastack-api-key-here" ``` -------------------------------- ### Set SambaNova API Key Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/text_embedding_sambanova.ipynb Set your SambaNova API key as an environment variable for authentication. ```bash export SAMBANOVA_API_KEY="your-api-key-here" ``` -------------------------------- ### Multimodal Input with ChatSambaNova Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/chat_sambanova.ipynb Initializes a ChatSambaNova model for multimodal processing and invokes it with a message containing both text and an image URL. This allows the LLM to understand and respond to visual information. ```python import base64 import httpx image_url = ( "https://images.pexels.com/photos/147411/italy-mountains-dawn-daybreak-147411.jpeg" ) image_data = base64.b64encode(httpx.get(image_url).content).decode("utf-8") message = HumanMessage( content=[ {"type": "text", "text": "describe the weather in this image in 1 sentence"}, { "type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_data}"}, }, ], ) response = multimodal_llm.invoke([message]) print(response.content) ``` -------------------------------- ### Asynchronous Chat with ChatSambaNova Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/usage_guide.ipynb Demonstrates how to perform asynchronous chat operations using the `ainvoke` method, suitable for non-blocking applications. ```python import asyncio async def async_chat_example(): llm = ChatSambaNova(model="Meta-Llama-3.3-70B-Instruct", max_tokens=512) response = await llm.ainvoke("What are the three laws of robotics?") print(response.content) ``` -------------------------------- ### Include Raw Response Alongside Parsed Output Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/usage_guide.ipynb Shows how to retrieve both the parsed structured output and the raw LLM response using `include_raw=True`. This is helpful for debugging or when you need access to metadata like tool calls or usage statistics. ```python # Include raw response alongside parsed output structured_llm = llm.with_structured_output(Joke, include_raw=True) result = structured_llm.invoke("Tell me a joke about AI") print("Parsed joke:") print(f" Setup: {result['parsed'].setup}") print(f" Punchline: {result['parsed'].punchline}") print("\nRaw response:") print(f" Tool calls: {result['raw'].tool_calls}") print(f" Usage: {result['raw'].response_metadata.get('usage', {})}") ``` -------------------------------- ### Multi-turn Conversation with Tool Execution Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/usage_guide.ipynb Demonstrates a multi-turn conversation where the model first identifies the need for tool use based on a user's query. The `ToolMessage` is used to return the tool's output back to the model for further processing. ```python # Multi-turn conversation with tool execution from langchain_core.messages import ToolMessage messages = [HumanMessage(content="What time is it, and what is 15 + 27?")] # First call - model decides to use tools response = llm_with_tools.invoke(messages) messages.append(response) print("Tool calls requested:") for tool_call in response.tool_calls: print(f" - {tool_call['name']}: {tool_call['args']}") ``` -------------------------------- ### Serialize and Deserialize Langchain Model Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/usage_guide.ipynb Demonstrates how to serialize a Langchain model configuration using `dumpd` and load it back using `load`. This is useful for saving and restoring model states. ```python from langchain_core.load import dumpd, load from langchain_community.chat_models import ChatSambaNova # Create and serialize a model llm = ChatSambaNova( model="Meta-Llama-3.3-70B-Instruct", max_tokens=1024, temperature=0.5, top_p=0.95 ) # Example of how dumpd and load would be used (actual serialization/deserialization not shown here) # serialized_llm = dumpd(llm) # loaded_llm = load(serialized_llm) ``` -------------------------------- ### Asynchronous Streaming with ChatSambaNova Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/usage_guide.ipynb Shows how to handle streaming responses asynchronously using the `astream` method. This is useful for non-blocking operations in async applications. The model must be initialized with `streaming=True`. ```python async def async_streaming_example(): llm = ChatSambaNova(model="Meta-Llama-3.3-70B-Instruct", streaming=True) messages = [ SystemMessage(content="You are a creative writer."), HumanMessage(content="Write a haiku about artificial intelligence."), ] print("Async streaming response:") async for chunk in llm.astream(messages): print(chunk.content, end="", flush=True) print("\n\nDone!") await async_streaming_example() ``` -------------------------------- ### Bind Tools to ChatSambaNova Model Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/usage_guide.ipynb Bind a list of defined tools to a ChatSambaNova model instance using `bind_tools`. This prepares the model to recognize and invoke these tools when prompted. ```python # Bind tools to the model llm = ChatSambaNova(model="gpt-oss-120b", temperature=0.0) llm_with_tools = llm.bind_tools([get_current_time, add, multiply]) # Query that requires tool use response = llm_with_tools.invoke("What is 25 multiplied by 17?") print("Response:", response.content) print("\nTool calls:") for tool_call in response.tool_calls: print(f" - {tool_call['name']}: {tool_call['args']}") ``` -------------------------------- ### Asynchronous Batching with ChatSambaNova Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/usage_guide.ipynb Shows how to perform asynchronous batch requests using an async function. This is useful for non-blocking operations in asynchronous applications. ```python async def async_batch_example(): llm = ChatSambaNova(model="Meta-Llama-3.3-70B-Instruct", max_tokens=100) prompts = ["What is Python?", "What is JavaScript?", "What is Rust?"] responses = await llm.abatch(prompts) print("Async batch responses:") for i, (prompt, response) in enumerate(zip(prompts, responses), 1): print(f"\n{i}. Q: {prompt}") print(f" A: {response.content[:100]}...") await async_batch_example() ``` -------------------------------- ### Optional: Set LangSmith API Key for Tracing Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/chat_sambanova.ipynb Enable automated tracing of model calls by setting LangSmith API keys. Uncomment and provide your LangSmith API key to activate this feature. ```python # os.environ["LANGCHAIN_TRACING_V2"] = "true" # os.environ["LANGCHAIN_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ") ``` -------------------------------- ### Invoke LLM with Tools for Date Retrieval Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/chat_sambanova.ipynb Binds a list of tools to a Langchain LLM and invokes it with a human message requesting a date. The code iteratively calls tools based on the LLM's response until a final answer is generated. This is useful for LLM-powered applications that need to access real-time information. ```python llm_with_tools = llm.bind_tools(tools=tools) messages = [ HumanMessage( content="I need to schedule a meeting for two weeks from today. " "Can you tell me the exact date of the meeting?" ) ] ``` ```python response = llm_with_tools.invoke(messages) while len(response.tool_calls) > 0: print(f"Intermediate model response: {response.tool_calls}") messages.append(response) messages = invoke_tools(response.tool_calls, messages) response = llm_with_tools.invoke(messages) print(f"final response: {response.content}") ``` -------------------------------- ### Pydantic Model for Complex Structured Output - Person Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/usage_guide.ipynb Illustrates generating complex structured data, including lists, using Pydantic models with `with_structured_output`. This is useful for creating detailed profiles or records. The LLM will attempt to populate all fields based on the prompt. ```python # Complex structured output example from typing import List class Person(BaseModel): """Information about a person.""" name: str = Field(description="The person's full name") age: int = Field(description="The person's age in years") occupation: str = Field(description="The person's occupation") hobbies: List[str] = Field(description="List of the person's hobbies") structured_llm = llm.with_structured_output(Person) person = structured_llm.invoke( "Create a profile for a fictional software engineer named Alex who loves hiking and photography" ) print(f"Name: {person.name}") print(f"Age: {person.age}") print(f"Occupation: {person.occupation}") print(f"Hobbies: {', '.join(person.hobbies)}") ``` -------------------------------- ### Serialize and Deserialize Chat Model Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/usage_guide.ipynb Demonstrates how to serialize a ChatSambaNova model to a dictionary and then deserialize it back. Requires valid_namespaces for partner integrations. ```python serialized = dumpd(llm) print("Serialized model config:") print(serialized) # Deserialize back to model # Note: Requires valid_namespaces for partner integrations loaded_llm = load( serialized, valid_namespaces=["langchain_sambanova"], secrets_from_env=True, allowed_objects=[ChatSambaNova], ) print("\nLoaded model type:", type(loaded_llm)) # Use the loaded model response = loaded_llm.invoke("Hello, testing serialization!") print("Response:", response.content) ``` -------------------------------- ### Set SambaNova API Key Environment Variable Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/chat_sambanova.ipynb Configure your SambaNova API key as an environment variable. This is required for authentication with SambaNova Cloud services. The code prompts for the key if it's not already set. ```python import getpass import os if not os.getenv("SAMBANOVA_API_KEY"): os.environ["SAMBANOVA_API_KEY"] = getpass.getpass( "Enter your SambaNova Cloud API key: " ) ``` -------------------------------- ### Generate Chat Completions Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/chat_sambanova.ipynb Use the instantiated ChatSambaNova model to generate chat completions from a list of messages. The messages can include system and human prompts. ```python messages = [ ( "system", "You are a helpful assistant that translates English to French. " "Translate the user sentence.", ), ("human", "I love programming."), ] ai_msg = llm.invoke(messages) ai_msg ``` -------------------------------- ### Streaming with Usage Metadata Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/usage_guide.ipynb This snippet shows how to enable and retrieve usage metadata (input, output, and total tokens) when streaming responses from a ChatSambaNova model. Ensure 'include_usage' is set in stream_options. ```python llm = ChatSambaNova( model="Meta-Llama-3.3-70B-Instruct", streaming=True, stream_options={\"include_usage\": True}, ) prompt = "Explain quantum computing in simple terms." full_response = "" usage_metadata = None for chunk in llm.stream(prompt): full_response += chunk.content print(chunk.content, end="", flush=True) # Usage metadata is in the final chunk if chunk.usage_metadata: usage_metadata = chunk.usage_metadata print("\n\n--- Usage Metadata ---") print(f"Input tokens: {usage_metadata.get('input_tokens', 0)}") print(f"Output tokens: {usage_metadata.get('output_tokens', 0)}") print(f"Total tokens: {usage_metadata.get('total_tokens', 0)}") ``` -------------------------------- ### Embed Multiple Texts with SambaNovaEmbeddings Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/text_embedding_sambanova.ipynb Embed multiple text strings into vectors using the embed_documents method. ```python text2 = ( "LangGraph is a library for building stateful, multi-actor applications with LLMs" ) two_vectors = embeddings.embed_documents([text, text2]) for vector in two_vectors: print(str(vector)[:100]) # Show the first 100 characters of the vector ``` -------------------------------- ### Generate Structured Output with Pydantic and SambaNova LLM Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/chat_sambanova.ipynb Configures a Langchain LLM to output structured data using Pydantic models. This is useful for extracting specific information from LLM responses in a predictable format. ```python from pydantic import BaseModel, Field class Joke(BaseModel): """Joke to tell user.""" setup: str = Field(description="The setup of the joke") punchline: str = Field(description="The punchline to the joke") structured_llm = llm.with_structured_output(Joke) structured_llm.invoke("Tell me a joke about cats") ``` -------------------------------- ### Semantic Search with Chroma Vector Store Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/usage_guide.ipynb Performs a semantic similarity search on the Chroma vector store to find relevant documents. Adjust `k` for the number of results. ```python # Semantic search query = "How do I build AI applications?" results = vectorstore.similarity_search(query, k=2) print(f"Query: {query}\n") print("Top 2 relevant documents:") for i, doc in enumerate(results, 1): print(f"\n{i}. {doc.page_content}") print(f" Metadata: {doc.metadata}") ``` -------------------------------- ### Define Tools for Function Calling Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/usage_guide.ipynb Define custom tools using the @tool decorator for use with Langchain and SambaNova models. Each tool should have a clear name, docstring, and type hints for arguments and return values. ```python from datetime import datetime from langchain_core.tools import tool # Define tools using the @tool decorator @tool def get_current_time() -> str: """Get the current time.""" return datetime.now().strftime("%H:%M:%S") @tool def add(a: int, b: int) -> int: """Add two numbers together.""" return a + b @tool def multiply(a: int, b: int) -> int: """Multiply two numbers together.""" return a * b ``` -------------------------------- ### Embed Multiple Documents with SambaNova Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/usage_guide.ipynb Embeds a list of documents using the initialized SambaNovaEmbeddings model. Useful for batch processing. ```python # Embed multiple documents documents = [ "Machine learning is a subset of artificial intelligence.", "Deep learning uses neural networks with multiple layers.", "Natural language processing helps computers understand text.", "Computer vision enables machines to interpret images.", ] doc_embeddings = embeddings.embed_documents(documents) print(f"Number of documents: {len(documents)}") print(f"Number of embeddings: {len(doc_embeddings)}") print(f"Each embedding dimension: {len(doc_embeddings[0])}") ``` -------------------------------- ### Synchronous Chat with Message Objects Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/usage_guide.ipynb Use LangChain's message objects (SystemMessage, HumanMessage) for more structured and controlled synchronous chat interactions. ```python # Using message objects for more control messages = [ SystemMessage(content="You are a helpful assistant that speaks like a pirate."), HumanMessage(content="Tell me about machine learning."), ] response = llm.invoke(messages) print(response.content) ``` -------------------------------- ### Compare Multiple Images Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/usage_guide.ipynb Send multiple images within a single HumanMessage to compare them. The model can identify differences or similarities between the provided images. ```python # Multiple images in conversation message = HumanMessage( content=[ { "type": "text", "text": "Compare these two images. What are the main differences?", }, { "type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_b64}"}, }, { "type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_b64}"}, }, ] ) response = llm_vision.invoke([message]) print(response.content) ``` -------------------------------- ### Embed Single Text with SambaNovaEmbeddings Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/text_embedding_sambanova.ipynb Directly embed a single text string into a vector using the embed_query method. ```python single_vector = embeddings.embed_query(text) print(str(single_vector)[:100]) # Show the first 100 characters of the vector ``` -------------------------------- ### Enable High-Effort Reasoning Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/usage_guide.ipynb Utilize the ChatSambaNova model with reasoning enabled by setting `reasoning_effort` to 'high'. This is useful for complex logic puzzles and problems requiring detailed thought processes. ```python # Using gpt-oss-120b with reasoning llm_reasoning = ChatSambaNova( model="gpt-oss-120b", max_tokens=2048, reasoning_effort="high", # Enable high-effort reasoning ) prompt = """Solve this logic puzzle: Three friends - Alice, Bob, and Charlie - each have a different pet: a cat, a dog, and a bird. - Alice is allergic to cats - Bob doesn't have a bird - Charlie doesn't have a dog Who has which pet?""" response = llm_reasoning.invoke(prompt) print(response.content) ``` -------------------------------- ### Create Message with Single Image Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/usage_guide.ipynb Construct a HumanMessage containing text and a single image for vision-based analysis. Ensure the image is base64 encoded. ```python message = HumanMessage( content=[ {"type": "text", "text": "What's in this image? Describe it in detail."}, { "type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_b64}"}, }, ] ) response = llm_vision.invoke([message]) print(response.content) ``` -------------------------------- ### Execute Tools and Add Results to Conversation Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/usage_guide.ipynb This snippet shows how to iterate through tool calls, invoke them, and append their results as ToolMessage objects to the conversation history for the model's next turn. It's useful when the LLM needs to perform actions or fetch information before generating a final response. ```python for tool_call in response.tool_calls: if tool_call["name"] == "get_current_time": result = get_current_time.invoke({}) elif tool_call["name"] == "add": result = add.invoke(tool_call["args"]) messages.append(ToolMessage(content=str(result), tool_call_id=tool_call["id"])) # Second call - model uses tool results to answer final_response = llm_with_tools.invoke(messages) print("\nFinal response:", final_response) ``` -------------------------------- ### Print AI Message Content Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/chat_sambanova.ipynb Extract and print the content from the AI's response message. ```python print(ai_msg.content) ``` -------------------------------- ### Python Decorator for Timing Functions Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/usage_guide.ipynb A decorator that measures and prints the execution time of a function. It handles arbitrary arguments using `*args` and `**kwargs`. ```python import time def timer_decorator(func): def wrapper(*args, **kwargs): start_time = time.time() result = func(*args, **kwargs) end_time = time.time() print(f"Function {func.__name__} took {end_time - start_time:.2f} seconds to execute.") return result return wrapper @timer_decorator def example_function(): time.sleep(2) # Simulate some work print("Function executed.") example_function() ``` -------------------------------- ### Python Decorator for Async Functions Source: https://github.com/sambanova/langchain-sambanova/blob/main/docs/usage_guide.ipynb An asynchronous decorator that adds behavior before and after an async function call. The wrapper function must be defined as `async def` and use `await` for the decorated function. ```python import asyncio def my_decorator(func): async def wrapper(*args, **kwargs): print("Before the function is called.") result = await func(*args, **kwargs) print("After the function is called.") return result return wrapper @my_decorator async def say_hello(name): await asyncio.sleep(1) # Simulate some async work print(f"Hello, {name}!") async def main(): await say_hello("John") asyncio.run(main()) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.