These examples use the standard OpenAI Python client with Giotto's OpenAI-compatible endpoint. Complete Make your first API request with Python first.
Set up a reusable client
import os
from openai import OpenAI
client = OpenAI(
base_url=os.environ["GIOTTO_OPENAI_BASE_URL"],
api_key=os.environ["GIOTTO_API_KEY"],
)
model_id = client.models.list().data[0].idFor a deployed application, select a reviewed model ID rather than relying on list order. The examples below assume client and model_id are set.
Use case 1: Answer user questions
Wrap a Chat Completions call in an application function:
def answer_question(question: str) -> str:
completion = client.chat.completions.create(
model=model_id,
messages=[
{
"role": "system",
"content": "Answer concisely. Say when the available information is insufficient.",
},
{"role": "user", "content": question},
],
)
return completion.choices[0].message.content or ""
print(answer_question("What is the difference between a list and a tuple?"))Validate user input, set application timeouts, and handle API errors. Generated answers can be wrong, so verify important claims against reliable sources.
Use case 2: Summarize content your application has retrieved
Fetch a document or web page in your application, extract the relevant text, and send that text to the model. Supplying a URL in a prompt alone does not make the model visit the page.
page_text = "Python is a programming language used for many kinds of software."
completion = client.chat.completions.create(
model=model_id,
messages=[
{
"role": "system",
"content": "Summarize only the supplied source text. Do not add unsupported facts.",
},
{
"role": "user",
"content": f"Source text:\n{page_text}\n\nSummarize in one sentence.",
},
],
)
print(completion.choices[0].message.content)Apply your application's URL access and data-handling rules before retrieving or sending external content. Show users the source you provided when they need to check the answer.
Use case 3: Display generated text as it arrives
For a chat interface, set stream=True and read text deltas:
stream = client.chat.completions.create(
model=model_id,
messages=[{"role": "user", "content": "Write a short welcome message."}],
stream=True,
)
for chunk in stream:
if chunk.choices:
print(chunk.choices[0].delta.content or "", end="", flush=True)
print()Streaming can also contain non-text deltas, such as tool calls, depending on the model and request. A streaming response keeps a concurrency slot until it finishes or closes. Close an abandoned stream promptly.
Use case 4: Ask a question about an image
Send a bar-chart image with a question and print the model's answer. Download the sample bar chartand save it as bar-chart-example.png beside your Python script.
The sample image is titled “Favorite Graphs.” and is available from Wikimedia Commons.
Example request
completion = client.chat.completions.create(
model=model_id,
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "What is this chart's title, and which bar is highest?",
},
{
"type": "image_url",
"image_url": {
"url": "https://docs.giotto.ai/hc/article_attachments/40515177175314"
},
},
],
}
],
)
print(completion.choices[0].message.content)Expected result
The answer should identify the title as “Favorite Graphs” and “Line Graphs” as the highest bar, with a value of 9. This request was verified against the stage giotto-alto model; the precise wording may vary.
The SDK sends requests containing files as multipart/form-data. Keep the image on the server side of your application; do not expose API credentials in browser code.
Production considerations
- Keep
GIOTTO_API_KEYon the server; do not send it to a browser. - Use a model returned by
client.models.list()and check its capabilities for your use case. - Handle authentication, balance, rate-limit, and service errors. Read
Retry-Afterafter a gateway429. - Avoid logging secrets or sensitive prompt content.
- Test optional features, such as image input or tool calls, with the selected model before relying on them. The gateway does not guarantee full OpenAI API feature parity.