This guide helps you get started with AI/ML API embedding models using LangChain. For detailed documentation on AimlapiEmbeddings features and configuration options, please refer to the API reference.

Overview

Integration details

ClassPackageLocalJS supportDownloadsVersion
AimlapiEmbeddingslangchain-aimlapiPyPI - DownloadsPyPI - Version

Setup

To access AI/ML API embedding models you’ll need to create an account, get an API key, and install the langchain-aimlapi integration package.

Credentials

Head to aimlapi.com to sign up and generate an API key. Once you’ve done this set the AIMLAPI_API_KEY environment variable:
import getpass
import os

if not os.getenv("AIMLAPI_API_KEY"):
    os.environ["AIMLAPI_API_KEY"] = getpass.getpass("Enter your AI/ML API key: ")
To enable automated tracing of your model calls, set your LangSmith API key:
# os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
# os.environ["LANGSMITH_TRACING"] = "true"

Installation

The LangChain AI/ML API integration lives in the langchain-aimlapi package:
%pip install -qU langchain-aimlapi

Instantiation

Now we can instantiate our embeddings model and perform embedding operations:
from langchain_aimlapi import AimlapiEmbeddings

embeddings = AimlapiEmbeddings(
    model="text-embedding-ada-002",
)

Indexing and Retrieval

Embedding models are often used in retrieval-augmented generation (RAG) flows. Below is how to index and retrieve data using the embeddings object we initialized above with InMemoryVectorStore.
from langchain_core.vectorstores import InMemoryVectorStore

text = "LangChain is the framework for building context-aware reasoning applications"

vectorstore = InMemoryVectorStore.from_texts(
    [text],
    embedding=embeddings,
)

retriever = vectorstore.as_retriever()

retrieved_documents = retriever.invoke("What is LangChain?")
retrieved_documents[0].page_content
'LangChain is the framework for building context-aware reasoning applications'

Direct usage

You can directly call embed_query and embed_documents for custom embedding scenarios.

Embed single text

single_vector = embeddings.embed_query(text)
print(str(single_vector)[:100])

Embed multiple texts

text2 = "LangGraph is a library for building stateful, multi-actor applications with LLMs"

vectors = embeddings.embed_documents([text, text2])
for vector in vectors:
    print(str(vector)[:100])

API reference

For detailed documentation on AimlapiEmbeddings features and configuration options, please refer to the API reference.