LangChain
LangChain is an open-source framework that simplifies the creation of applications using large language models (LLMs). This framework provides a standard interface for chains, integrations with other tools, and end-to-end chains for common applications. Compass APIs support LangChain SDK integrations.
To get started, refer to the LangChain quickstart guide.
For a list of available endpoints, see the LangChain API Reference docs.
An example of LangChain integration using Python is provided below.
Chat Completion Example
import os
from langchain.schema import HumanMessage, SystemMessage
from langchain_openai import ChatOpenAI
# "OPENAI_API_KEY" need to be set due to langchain SDK, you can use any dummy value here to bypass
os.environ["OPENAI_API_KEY"] = "XXX"
chat = ChatOpenAI(
model="gpt-4o",
openai_api_base="https://api.core42.ai/v1",
# while Compass API use different Authentication header "api-key" that you should set in the headers
default_headers={"api-key": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"},
)
messages = [
SystemMessage(
content="You are a helpful assistant that translates English to French."
),
HumanMessage(
content="Translate this sentence from English to French. I love programming."
),
]
res = chat.invoke(messages)
print(res)
Embeddings Example - OpenAI
import os
from langchain_openai import OpenAIEmbeddings
# "OPENAI_API_KEY" need to be set due to langchain SDK, you can use any dummy valuehere to bypass
os.environ["OPENAI_API_KEY"] = "XXX"
embed = OpenAIEmbeddings(
model="text-embedding-3-large",
openai_api_base="https://api.core42.ai/v1",
# while Compass API use different Authentication header "api-key" , you should set the custom headers for authentication
default_headers={"api-key": "your api key"}
)
# Define the text to be embedded
text = "This is a test query."
# Invoke the embedding method
embedding = embed.embed_query(text)
# Print the embedding
print(embedding)
Embeddings Example - Azure OpenAI
import os
from langchain_openai import AzureOpenAIEmbeddings
# "OPENAI_API_KEY" need to be set due to langchain SDK, you can use any dummy valuehere to bypass
os.environ["OPENAI_API_KEY"] = "XXX"
embed = AzureOpenAIEmbeddings(
openai_api_base="https://api.core42.ai/openai/deployments/{deployment-id}",
api_key="your api key",
)
# Define the text to be embedded
text = "This is a test query."
# Invoke the embedding method
embedding = embed.embed_query(text)
# Print the embedding
print(embedding)