π Chat ApiΒΆ
formats.chat_api
Explanation about ChatAPIFormatΒΆ
Formats output for LLM APIs using OpenAIβs chat schema.
Many API services use OpenAIβs chat format as a standard for conversational models. OpenAIFormat prepares the output in this API-compatible format, converting input instances into OpenAIβs structured chat format, which supports both text and multimedia elements, like images.
The formatted output can be easily converted to a dictionary using json.loads() to make it ready for direct use with OpenAIβs API.
- Example:
Given an input instance:
{ "source": "<img src='https://example.com/image1.jpg'>What's in this image?", "target": "A dog", "instruction": "Help the user.", },When processed by:
system_format = OpenAIFormat()The resulting formatted output is:
{ "target": "A dog", "source": '[{"role": "system", "content": "Help the user."}, ' '{"role": "user", "content": [{"type": "image_url", ' '"image_url": {"url": "https://example.com/image1.jpg", "detail": "low"}}, ' '{"type": "text", "text": "What\'s in this image?"}]}]' }This source field is a JSON-formatted string. To make it ready for OpenAIβs API, you can convert it to a dictionary using json.loads():
import json messages = json.loads(formatted_output["source"]) response = client.chat.completions.create( model="gpt-4o", messages=messages, )The resulting messages is now a dictionary ready for sending to the OpenAI API.
Read more about catalog usage here.