Skip to main content

LangChain Integration Guide

APIpieLangChain

This guide will walk you through integrating LangChain with APIpie, enabling you to build powerful AI applications using language models with a flexible and robust framework.

What is LangChain?

LangChain is a popular framework for developing applications powered by language models. It enables applications that:

  • Connect language models to various data sources and knowledge bases
  • Create chains of operations for complex reasoning and text processing
  • Build agents that can make decisions and take actions
  • Develop sophisticated retrieval-augmented generation (RAG) applications

By connecting LangChain to APIpie, you unlock access to a wide range of powerful models, enhanced context windows, and cost-efficient AI capabilities.


Integration Steps

1. Create an APIpie Account

2. Add Credit

3. Generate an API Key

  • API Key Management: APIpie API Keys
  • Create a new API key for use with LangChain.

4. Install LangChain

For JavaScript/TypeScript:

npm install -S langchain
# or
yarn add langchain
# or
pnpm add langchain

For Python:

pip install -U langchain
pip install python-dotenv # Optional, for .env file support

5. Configure LangChain for APIpie

LangChain is designed to work with OpenAI-compatible endpoints like APIpie. You just need to set the right base URL and API key:

For JavaScript/TypeScript:

export OPENAI_API_KEY="your-APIpie-key-here"
export OPENAI_API_BASE_URL="https://apipie.ai/v1"

For Python:

export OPENAI_API_KEY="your-APIpie-key-here"
export OPENAI_API_BASE="https://apipie.ai/v1"
tip

Add these environment variables to your shell profile or a .env file for persistence.


Key Features

  • Modular Framework: LangChain's modular design makes it easy to swap components as needed.
  • Rich Ecosystem: Access a wide variety of tools, retrievers, and memory components.
  • Model Agnostic: Work with any language model through consistent interfaces.
  • Robust Abstractions: Built-in patterns for common AI application architectures.
  • Model Flexibility: Access APIpie's latest tools capable models.
  • Cost Efficiency: Control your spending with APIpie's transparent pricing.

Example Workflows

Application TypeWhat LangChain Helps You Build
Question AnsweringSystems that answer questions using specific documents or data
ChatbotsInteractive conversational agents with memory and context
Data AnalysisAnalyze and extract insights from structured or unstructured data
Content GenerationCreate articles, summaries, marketing copy with specific styles
Function CallingUse LLMs to determine when and how to call external functions
AgentsAutonomous systems that can reason and take actions on their own

Using LangChain with APIpie

JavaScript/TypeScript

const chat = new ChatOpenAI(
{
modelName: '<model_name>', // e.g., 'gpt-4o-mini
temperature: 0.8,
streaming: true,
openAIApiKey: '${APIPIE_API_KEY}',
},
{
basePath: 'https://apipie.ai/v1',
},
);

Python

from langchain.chat_models import ChatOpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from os import getenv
from dotenv import load_dotenv

load_dotenv()

template = """Question: {question}
Answer: Answer like a science teacher."""

prompt = PromptTemplate(template=template, input_variables=["question"])

llm = ChatOpenAI(
openai_api_key=getenv("APIPIE_API_KEY"),
openai_api_base=getenv("APIPIE_BASE_URL"),
model_name="<model_name>",
)

llm_chain = LLMChain(prompt=prompt, llm=llm)

question = "Why is the sky blue?"

print(llm_chain.run(question))

Building Interactive AI Apps with Streamlit and LangChain

Streamlit is a powerful Python library that makes it easy to create beautiful, interactive web applications for machine learning and data science. Combined with LangChain and APIpie, you can quickly build sophisticated AI applications with minimal code.

Why Use Streamlit with LangChain?

  • Rapid Development: Build full-featured AI applications in hours, not weeks
  • Interactive UI: Create rich, responsive interfaces without frontend expertise
  • Deployment Simplicity: Easy deployment options including Streamlit Cloud
  • Real-Time Feedback: Get immediate visual feedback as you develop
  • Component Ecosystem: Access a wide variety of pre-built components

Simple Streamlit Chat Interface

import streamlit as st
from langchain.chat_models import ChatOpenAI
from components.Sidebar import sidebar
from shared import constants
from langchain.schema import (
HumanMessage,
)

st.title("Langchain Streamlit App")

# Add a sidebar for configuration
api_key, selected_model = sidebar(constants.APIPIE_DEFAULT_CHAT_MODEL)

def generate_response(input_text):
chat = ChatOpenAI(
temperature=0.7,
model=selected_model,
openai_api_key=api_key,
openai_api_base=constants.APIPIE_API_BASE,
)
resp = chat([HumanMessage(content=input_text)])
st.write(resp.content)

with st.form("test_form"):
text = st.text_area(
"Input Text:", "Why is the sky blue?"
)
submitted = st.form_submit_button("Enter")
if submitted:
with st.spinner("Generating response..."):
generate_response(text)

Advanced Streamlit Features

For more sophisticated applications, you can enhance your Streamlit app with:

  • Chat History: Store and display conversation history
  • File Uploading: Process documents with st.file_uploader
  • Interactive Visualizations: Display charts and graphs of LLM analysis
  • Session State: Maintain state between reruns with st.session_state
  • Multipage Apps: Create applications with multiple pages

Deploying Your Streamlit App

Once you've built your LangChain + Streamlit application:

  1. Local Development: Run with streamlit run app.py
  2. Streamlit Cloud: Deploy directly from GitHub
  3. Docker Containers: Package for deployment anywhere
  4. Cloud Providers: Deploy to AWS, GCP, Azure, or other providers

Installation Requirements

pip install streamlit langchain python-dotenv

Troubleshooting & FAQ

  • Which models are supported?
    Any tools capable model available via APIpie's OpenAI-compatible endpoint.
  • How do I persist my API key and endpoint?
    Add the export lines to your shell profile or use a .env file.
  • Can I use streaming with LangChain?
    Yes, set streaming=True in your ChatOpenAI configuration and use the appropriate callbacks.

For more, see the LangChain.js GitHub or LangChain Python GitHub.


Support

If you encounter any issues during the integration process, please reach out on APIpie Discord for assistance.