The landscape of artificial intelligence is shifting. For a long time, accessing state-of-the-art Large Language Models (LLMs) meant relying entirely on proprietary, closed-source APIs. But a new paradigm is taking over: open-weight LLMs.
Models like Meta's Llama 3, Mistral's Mixtral, and Google's Gemma have changed the game. By making their model weights publicly available, they’ve democratized access to powerful AI. But down a 70B parameter model and running it locally isn't always practical. That's where API integration comes in.
In this post, we'll explore why open-weight models matter, how to integrate them into your applications via API, and look at practical code examples to get you started.
Before we dive into the code, let's talk about why developers are flocking to open-weight models. It’s not just about the price tag (though that helps).
Here is the best-kept secret in the open-weight ecosystem: most modern LLM API providers are compatible with the OpenAI API spec.
This means you don't need to learn a new SDK for every open-weight provider. If you know how to call OpenAI's gpt-4
, you already know how to call Mistral's Mixtral-8x7B
. You simply change the base_url
and the model
name.
To get started, you will need:
Let's install the SDK:
pip install openai
Let's look at how to integrate an open-weight model into a Python application. We'll use the Mistral-7B-Instruct model as an example, but the pattern applies to any OpenAI-compatible endpoint.
In this example, we configure the OpenAI client to point to an open-weight API provider instead of OpenAI's servers.
from openai import OpenAI
client = OpenAI(
base_url="https://api.together.xyz/v1", # Example provider
api_key="your-api-key-here"
)
response = client.chat.completions.create(
model="mistralai/Mistral-7B-Instruct-v0.2", # Open-weight model ID
messages=[
{"role": "system", "content": "You are a helpful coding assistant."},
{"role": "user", "content": "Write a Python function to reverse a string."}
]
)
print(response.choices[0].message.content)
Notice how the code structure is identical to calling GPT-3.5 or GPT-4? The only differences are the base_url
and the model
identifier. This standardization is what makes open-weight API integration so frictionless.
For chat applications, waiting for the full response to generate can lead to a poor user experience. Streaming allows tokens to be sent to the user as they are generated.
Here is how you implement streaming with an open-weight model:
from openai import OpenAI
client = OpenAI(
base_url="https://api.together.xyz/v1",
api_key="your-api-key-here"
)
stream = client.chat.completions.create(
model="mistralai/Mistral-7B-Instruct-v0.2",
messages=[
{"role": "user", "content": "Explain the concept of recursion in programming."}
],
stream=True,
)
for chunk in stream:
if chunk.choices[0].delta.content is not None:
print(chunk.choices[0].delta.content, end="")
Open-weight models are rapidly catching up to proprietary models in terms of advanced features like tool use. If you want your LLM to trigger external APIs or functions, you can define tools just like you would with OpenAI.
from openai import OpenAI
client = OpenAI(
base_url="https://api.together.xyz/v1",
api_key="your-api-key-here"
)
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "The city, e.g., San Francisco"},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
},
"required": ["location"]
}
}
}
]
response = client.chat.completions.create(
model="mistralai/Mistral-7B-Instruct-v0.2",
messages=[{"role": "user", "content": "What's the weather like in London?"}],
tools=tools,
tool_choice="auto"
)
if response.choices[0].message.tool_calls:
tool_call = response.choices[0].message.tool_calls[0]
print(f"Function to call: {tool_call.function.name}")
print(f"Arguments: {tool_call.function.arguments}")
The era of open-weight LLMs is here, and it’s never been easier to integrate them into your tech stack. By leveraging the OpenAI API specification, developers can swap out massive, expensive proprietary models for highly capable, cost-effective open-weight alternatives with just a few lines of code.
Whether you're building a chatbot, an automated content pipeline, or a complex agentic workflow, open-weight models offer the flexibility, privacy, and control that modern applications demand.
If you're looking for a streamlined way to manage, deploy, and integrate open-weight LLMs into your applications, check out NovaStack. NovaStack provides the infrastructure and tooling to make working with open-weight models seamless, allowing you to focus on building great AI-powered products rather than managing infrastructure.