Skip to main content

Python examples

You can use the models within programming languages conveniently via existing libraries that support the OpenAI API. Therefore, mittwald’s AI hosting can often be used as a drop-in replacement.

For the following examples, first install the required libraries using a Python package manager and store the API key generated in mStudio in a .env file:

pip install python-dotenv openai langchain-openai
echo 'OPENAI_API_KEY="sk-…"' > .env

Then, you can access a model using the OpenAI package:

from openai import OpenAI
from dotenv import load_dotenv

# Load .env file
load_dotenv()

# Initialize client with custom host and key from environment
client = OpenAI(
base_url="https://llm.aihosting.mittwald.de/v1"
)

# Make a simple call
response = client.chat.completions.create(
model="Mistral-Small-3.2-24B-Instruct",
temperature = 0.15,
messages=[
{"role": "user", "content": "Moin and hello!"}
]
)

print(response.choices[0].message.content)

Alternatively, you can also use langchain:

from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage

# Load .env file
load_dotenv()

# Initialize client with custom host and key from environment
chat = ChatOpenAI(
model="Mistral-Small-3.2-24B-Instruct",
base_url="https://llm.aihosting.mittwald.de/v1",
temperature = 0.15
)

# Get response
response = chat.invoke([
HumanMessage(content="Moin and hello!")
])

print(response.content)