Skip to content
English
  • There are no suggestions because the search field is empty.

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

1 import os
2 from langchain.schema import HumanMessage, SystemMessage
3 from langchain_openai import ChatOpenAI
4
5 # "OPENAI_API_KEY" need to be set due to langchain SDK, you can use any dummy value here to bypass
6 os.environ["OPENAI_API_KEY"] = "XXX"
7 chat = ChatOpenAI(
8   model="gpt-4o",
9   openai_api_base="https://api.core42.ai/v1",
10   # while Compass API use different Authentication header "api-key" that you should set in the headers
11    default_headers={"api-key": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"},
12 )
13
14 messages = [
15    SystemMessage(
16        content="You are a helpful assistant that translates English to French."
17    ),
18    HumanMessage(
19        content="Translate this sentence from English to French. I love programming."
20    ),
21 ]
22
23
24 res = chat.invoke(messages)
25 print(res)

Embeddings Example - OpenAI

1  import os
2 from langchain_openai import OpenAIEmbeddings
3 # "OPENAI_API_KEY" need to be set due to langchain SDK, you can use any dummy
4 valuehere to bypass
5 os.environ["OPENAI_API_KEY"] = "XXX"
6 embed = OpenAIEmbeddings(
7 model="text-embedding-3-large",
8 openai_api_base="https://api.core42.ai/v1",
9 # while Compass API use different Authentication header "api-key" , you should set the
10 custom headers for authentication
11 default_headers={"api-key": "your api key"}
12 )
13 # Define the text to be embedded
14 text = "This is a test query."
15 # Invoke the embedding method
16 embedding = embed.embed_query(text)
17 # Print the embedding
18 print(embedding)

Embeddings Example - Azure OpenAI

1  import os
2 from langchain_openai import AzureOpenAIEmbeddings
3 # "OPENAI_API_KEY" need to be set due to langchain SDK, you can use any dummy
4 valuehere to bypass
5 os.environ["OPENAI_API_KEY"] = "XXX"
6 embed = AzureOpenAIEmbeddings(
7 openai_api_base="https://api.core42.ai/openai/deployments/{deployment-id}",
8 api_key="your api key",
9 )
10 # Define the text to be embedded
11 text = "This is a test query."
12 # Invoke the embedding method
13 embedding = embed.embed_query(text)
14 # Print the embedding
15 print(embedding)