Skip to content

LangChain Integration

UniCraft provides seamless integration with LangChain, allowing you to use multiple AI providers through LangChain’s unified interface.

LangChain is a powerful framework for building applications with Large Language Models (LLMs). UniCraft’s LangChain integration provides:

  • Multi-Provider Support: Access multiple AI providers through one interface
  • Smart Routing: Automatically choose the best model for your task
  • Cost Optimization: Reduce costs through intelligent model selection
  • Enhanced Reliability: Built-in failover and redundancy
  • LangChain Compatibility: Works with existing LangChain code
Terminal window
# Install LangChain and UniCraft
pip install langchain unicraft-langchain
# Or install specific components
pip install langchain-openai langchain-anthropic unicraft-langchain
Terminal window
# Set environment variables
export UNICRAFT_API_KEY="your-unicraft-api-key"
export OPENAI_API_KEY="your-openai-api-key"
export ANTHROPIC_API_KEY="your-anthropic-api-key"
from langchain.llms import UniCraft
from langchain.chat_models import ChatUniCraft
# Initialize UniCraft LLM
llm = UniCraft(
api_key="your-unicraft-api-key",
model="auto", # Smart routing
temperature=0.7
)
# Use with LangChain
response = llm("Hello, world!")
print(response)
from langchain.chat_models import ChatUniCraft
from langchain.schema import HumanMessage, SystemMessage
# Initialize chat model
chat = ChatUniCraft(
api_key="your-unicraft-api-key",
model="auto",
temperature=0.7
)
# Create messages
messages = [
SystemMessage(content="You are a helpful assistant."),
HumanMessage(content="Hello, world!")
]
# Get response
response = chat(messages)
print(response.content)
from langchain.llms import UniCraft
# Configure smart routing
llm = UniCraft(
api_key="your-unicraft-api-key",
model="auto",
routing_strategy="cost_optimized",
max_cost_per_request=0.01,
quality_threshold=0.8
)
response = llm("Analyze this data...")
# Specify preferred providers
llm = UniCraft(
api_key="your-unicraft-api-key",
model="auto",
providers=["openai", "anthropic"],
fallback_providers=["google"]
)
response = llm("Generate content...")
# Set cost limits
llm = UniCraft(
api_key="your-unicraft-api-key",
model="auto",
max_cost_per_request=0.05,
daily_cost_limit=100.00,
cost_optimization=True
)
response = llm("Write a summary...")
from langchain.llms import UniCraft
# Basic LLM
llm = UniCraft(
api_key="your-unicraft-api-key",
model="auto"
)
# With custom parameters
llm = UniCraft(
api_key="your-unicraft-api-key",
model="auto",
temperature=0.7,
max_tokens=1000,
top_p=0.9
)
from langchain.chat_models import ChatUniCraft
from langchain.schema import HumanMessage, SystemMessage, AIMessage
# Chat model
chat = ChatUniCraft(
api_key="your-unicraft-api-key",
model="auto"
)
# Conversation
messages = [
SystemMessage(content="You are a helpful assistant."),
HumanMessage(content="What is the capital of France?"),
AIMessage(content="The capital of France is Paris."),
HumanMessage(content="What is the population of Paris?")
]
response = chat(messages)
print(response.content)
from langchain.embeddings import UniCraftEmbeddings
# Embeddings
embeddings = UniCraftEmbeddings(
api_key="your-unicraft-api-key",
model="auto"
)
# Generate embeddings
text = "This is a sample text for embedding."
embedding = embeddings.embed_query(text)
print(embedding)
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
# Create prompt template
template = "Question: {question}\nAnswer:"
prompt = PromptTemplate(template=template, input_variables=["question"])
# Create chain
llm = UniCraft(api_key="your-unicraft-api-key", model="auto")
chain = LLMChain(llm=llm, prompt=prompt)
# Run chain
response = chain.run(question="What is the capital of France?")
print(response)
from langchain.chains import SimpleSequentialChain
# First chain
first_prompt = PromptTemplate(
input_variables=["product"],
template="What is the best name for a company that makes {product}?"
)
first_chain = LLMChain(llm=llm, prompt=first_prompt)
# Second chain
second_prompt = PromptTemplate(
input_variables=["company_name"],
template="Write a catchphrase for {company_name}"
)
second_chain = LLMChain(llm=llm, prompt=second_prompt)
# Sequential chain
overall_chain = SimpleSequentialChain(
chains=[first_chain, second_chain],
verbose=True
)
response = overall_chain.run("sustainable energy products")
print(response)
from langchain.agents import initialize_agent, Tool
from langchain.tools import DuckDuckGoSearchRun
# Define tools
search = DuckDuckGoSearchRun()
tools = [
Tool(
name="Search",
func=search.run,
description="Useful for searching the internet for current information"
)
]
# Initialize agent
llm = UniCraft(api_key="your-unicraft-api-key", model="auto")
agent = initialize_agent(
tools=tools,
llm=llm,
agent="zero-shot-react-description",
verbose=True
)
# Run agent
response = agent.run("What is the current weather in Paris?")
print(response)
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationChain
# Initialize memory
memory = ConversationBufferMemory()
# Create conversation chain
llm = UniCraft(api_key="your-unicraft-api-key", model="auto")
conversation = ConversationChain(
llm=llm,
memory=memory,
verbose=True
)
# Have conversation
response1 = conversation.predict(input="Hi, my name is Alice")
response2 = conversation.predict(input="What's my name?")
print(response2) # Should remember the name
from langchain.memory import ConversationSummaryMemory
from langchain.chains import ConversationChain
# Initialize summary memory
memory = ConversationSummaryMemory(llm=llm)
# Create conversation chain
conversation = ConversationChain(
llm=llm,
memory=memory,
verbose=True
)
# Long conversation
for i in range(10):
response = conversation.predict(input=f"Tell me about topic {i}")
print(f"Response {i}: {response}")
from langchain.document_loaders import TextLoader
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores import FAISS
from langchain.chains import RetrievalQA
# Load document
loader = TextLoader("document.txt")
documents = loader.load()
# Split text
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
texts = text_splitter.split_documents(documents)
# Create embeddings
embeddings = UniCraftEmbeddings(api_key="your-unicraft-api-key")
vectorstore = FAISS.from_documents(texts, embeddings)
# Create QA chain
llm = UniCraft(api_key="your-unicraft-api-key", model="auto")
qa = RetrievalQA.from_chain_type(
llm=llm,
chain_type="stuff",
retriever=vectorstore.as_retriever()
)
# Ask questions
response = qa.run("What is this document about?")
print(response)
from langchain.chains import MapReduceDocumentsChain, ReduceDocumentsChain
from langchain.chains.combine_documents.stuff import StuffDocumentsChain
from langchain.chains.llm import LLMChain
# Map chain
map_template = "Summarize this text: {docs}"
map_prompt = PromptTemplate.from_template(map_template)
map_chain = LLMChain(llm=llm, prompt=map_prompt)
# Reduce chain
reduce_template = "Combine these summaries: {docs}"
reduce_prompt = PromptTemplate.from_template(reduce_template)
reduce_chain = LLMChain(llm=llm, prompt=reduce_prompt)
# Combine chains
combine_documents_chain = StuffDocumentsChain(
llm_chain=reduce_chain,
document_variable_name="docs"
)
reduce_documents_chain = ReduceDocumentsChain(
combine_documents_chain=combine_documents_chain,
collapse_documents_chain=combine_documents_chain,
token_max=4000
)
map_reduce_chain = MapReduceDocumentsChain(
llm_chain=map_chain,
reduce_documents_chain=reduce_documents_chain,
document_variable_name="docs",
return_intermediate_steps=False
)
# Summarize document
summary = map_reduce_chain.run(documents)
print(summary)
from langchain.tools import BaseTool
from typing import Optional, Type
from pydantic import BaseModel, Field
class CalculatorInput(BaseModel):
expression: str = Field(description="Mathematical expression to evaluate")
class CalculatorTool(BaseTool):
name = "calculator"
description = "Useful for performing mathematical calculations"
args_schema: Type[BaseModel] = CalculatorInput
def _run(self, expression: str) -> str:
try:
result = eval(expression)
return str(result)
except Exception as e:
return f"Error: {str(e)}"
# Use custom tool
calculator = CalculatorTool()
result = calculator.run("2 + 2")
print(result) # Output: 4
from langchain.tools import tool
@tool
def get_weather(city: str) -> str:
"""Get the current weather for a city."""
# Mock weather data
weather_data = {
"paris": "Sunny, 22°C",
"london": "Cloudy, 18°C",
"tokyo": "Rainy, 25°C"
}
return weather_data.get(city.lower(), "Weather data not available")
# Use in agent
tools = [get_weather]
llm = UniCraft(api_key="your-unicraft-api-key", model="auto")
agent = initialize_agent(
tools=tools,
llm=llm,
agent="zero-shot-react-description",
verbose=True
)
response = agent.run("What's the weather like in Paris?")
print(response)
from langchain.cache import InMemoryCache
from langchain.globals import set_llm_cache
# Enable caching
set_llm_cache(InMemoryCache())
# First call (will be cached)
llm = UniCraft(api_key="your-unicraft-api-key", model="auto")
response1 = llm("What is the capital of France?")
# Second call (will use cache)
response2 = llm("What is the capital of France?")
# Process multiple inputs at once
inputs = [
"What is the capital of France?",
"What is the capital of Germany?",
"What is the capital of Italy?"
]
responses = llm.batch(inputs)
for i, response in enumerate(responses):
print(f"Question {i+1}: {response}")
# Stream responses
for chunk in llm.stream("Tell me a story"):
print(chunk, end="", flush=True)
from langchain.llms import UniCraft
from langchain.callbacks.manager import CallbackManagerForLLMRun
from typing import Optional, List, Any
class RetryUniCraft(UniCraft):
def _call(
self,
prompt: str,
stop: Optional[List[str]] = None,
run_manager: Optional[CallbackManagerForLLMRun] = None,
**kwargs: Any,
) -> str:
max_retries = 3
for attempt in range(max_retries):
try:
return super()._call(prompt, stop, run_manager, **kwargs)
except Exception as e:
if attempt == max_retries - 1:
raise e
print(f"Attempt {attempt + 1} failed: {e}")
time.sleep(2 ** attempt) # Exponential backoff
# Use retry LLM
llm = RetryUniCraft(api_key="your-unicraft-api-key", model="auto")
response = llm("Hello, world!")
from langchain.llms import UniCraft
from langchain.llms.fallback import FallbackLLM
# Primary LLM
primary_llm = UniCraft(
api_key="your-unicraft-api-key",
model="auto"
)
# Fallback LLM
fallback_llm = UniCraft(
api_key="your-unicraft-api-key",
model="gpt-3.5-turbo" # Specific model as fallback
)
# Create fallback chain
llm = FallbackLLM(
primary_llm=primary_llm,
fallback_llm=fallback_llm
)
response = llm("Hello, world!")
  • Use “auto” for most cases to leverage smart routing
  • Specify models only when you need specific capabilities
  • Consider cost vs. quality trade-offs
  • Implement retry logic for transient failures
  • Use fallback models for critical applications
  • Monitor and log errors
  • Use caching for repeated requests
  • Implement batch processing for multiple inputs
  • Monitor response times and costs
  • Store API keys securely
  • Validate input data
  • Monitor for unusual activity
  1. Authentication Errors

    • Check API key configuration
    • Verify environment variables
    • Test connection
  2. Model Not Available

    • Check provider configuration
    • Use “auto” for smart routing
    • Verify model availability
  3. Rate Limiting

    • Implement retry logic
    • Use multiple providers
    • Monitor usage
  4. High Costs

    • Use cost-optimized routing
    • Set cost limits
    • Monitor spending
  1. Enable verbose mode for debugging
  2. Use logging to track requests
  3. Test with simple prompts first
  4. Check UniCraft dashboard for usage

For support with LangChain integration:

After setting up LangChain integration:

  1. Test Components: Test LLMs, chat models, and embeddings
  2. Build Chains: Create simple and complex chains
  3. Implement Agents: Build intelligent agents with tools
  4. Optimize Performance: Implement caching and batch processing
  5. Monitor Usage: Set up monitoring and cost tracking