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.
LangChain is a popular framework for developing applications powered by language models. It enables applications that:
By connecting LangChain to APIpie, you unlock access to a wide range of powerful models, enhanced context windows, and cost-efficient AI capabilities.
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
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"
| Application Type | What LangChain Helps You Build |
|---|---|
| Question Answering | Systems that answer questions using specific documents or data |
| Chatbots | Interactive conversational agents with memory and context |
| Data Analysis | Analyze and extract insights from structured or unstructured data |
| Content Generation | Create articles, summaries, marketing copy with specific styles |
| Function Calling | Use LLMs to determine when and how to call external functions |
| Agents | Autonomous systems that can reason and take actions on their own |
const chat = new ChatOpenAI(
{
modelName: '<model_name>', // e.g., 'gpt-4o-mini
temperature: 0.8,
streaming: true,
openAIApiKey: '${APIPIE_API_KEY}',
configuration: {
baseURL: 'https://apipie.ai/v1',
},
},
{
basePath: 'https://apipie.ai/v1',
},
);
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))
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.
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)
For more sophisticated applications, you can enhance your Streamlit app with:
st.file_uploaderst.session_stateOnce you've built your LangChain + Streamlit application:
streamlit run app.pypip install streamlit langchain python-dotenv
export lines to your shell profile or use a .env file.streaming=True in your ChatOpenAI configuration and use the appropriate callbacks.For more, see the LangChain.js GitHub or LangChain Python GitHub.
If you encounter any issues during the integration process, please reach out on APIpie Discord for assistance.