This package connects ModelsLab and LangChain.
ModelsLab is an AI API platform for image, video, audio, 3D and LLM generation, with access to 10,000+ models through one API key. Paid plans start at $21/month.
The package contains:
ChatModelsLab: a chat model for the ModelsLab LLM API. The API is OpenAI-compatible, so the class supports streaming, async, tool calling and structured output (where the selected model supports them).ModelsLabImageGenerationTool: a LangChain tool that generates images with the ModelsLab text-to-image API.
pip install -U langchain-modelslabGet an API key at modelslab.com/dashboard/api-keys and set it:
export MODELSLAB_API_KEY="your-api-key"from langchain_modelslab import ChatModelsLab
model = ChatModelsLab(model="meta-llama-Llama-3.3-70B-Instruct-Turbo", temperature=0)
messages = [
("system", "You are a helpful assistant that translates English to French."),
("human", "I love programming."),
]
print(model.invoke(messages).content)
for chunk in model.stream(messages):
print(chunk.text, end="")Browse the LLM catalogue at modelslab.com/models and pass its model_id as model.
| Parameter | Env var | Default |
|---|---|---|
api_key |
MODELSLAB_API_KEY |
required |
base_url |
MODELSLAB_API_BASE |
https://modelslab.com/api/v7/llm |
from langchain_modelslab import ModelsLabImageGenerationTool
tool = ModelsLabImageGenerationTool(model_id="flux", width=1024, height=1024)
url = tool.invoke({"prompt": "a futuristic cityscape at sunset, highly detailed"})
print(url)The tool calls POST https://modelslab.com/api/v7/images/text-to-image. If the API queues the request, the tool polls POST /api/v7/images/fetch/{id} until the image is ready (poll_interval, max_wait). It returns the image URLs, one per line. Use extra_params for model-specific fields such as aspect_ratio.
from langchain.agents import create_agent
from langchain_modelslab import ChatModelsLab, ModelsLabImageGenerationTool
agent = create_agent(
model=ChatModelsLab(model="meta-llama-Llama-3.3-70B-Instruct-Turbo"),
tools=[ModelsLabImageGenerationTool()],
)
agent.invoke({"messages": [("human", "Make an image of a lighthouse in a storm")]})uv sync --all-groups
make test # unit tests, no network
make lint
MODELSLAB_API_KEY=... make integration_tests