Class Reference¶
- class SwitchAI(provider, model_name, api_key=None)[source]¶
The SwitchAI client class.
- Parameters:
provider (str) – The name of the provider to use.
model_name (str) – The name of the model to use.
api_key (str | None) – The API key to use, if not set it will be read from the environment variable. Defaults to None.
- chat(messages, temperature=1.0, max_tokens=None, tools=None, response_format=None, stream=False)[source]¶
Sends a chat request to the AI model and returns the response.
- Parameters:
messages (List[str | dict | ChatResponse]) – A list of messages to send to the model.
temperature (float | None) – Sampling temperature to use, between 0 and 2. Higher values like 0.8 will make
random (the output more)
deterministic. (while lower values like 0.2 will make it more focused and)
max_tokens (int | None) – The maximum number of tokens to generate. Defaults to None.
tools (List | None) – A list of tools the model may call.
response_format (Type[BaseModel] | None) – An object specifying the format that the model must output.
stream (bool | None) – Whether to stream the response.
- Returns:
The response from the model.
- Return type:
- embed(inputs)[source]¶
Embeds the input text using the AI model.
- Parameters:
inputs (str | Image | List[str | Image]) – The input text to embed. Can be a single string or a list of strings.
- Returns:
The response from the model.
- Return type:
TextEmbeddingResponse
- generate_image(prompt, n=1)[source]¶
Generate an image based on the provided prompt.
- Parameters:
prompt (str) – A text description of the desired image.
n (int) – The number of images to generate.
- Returns:
The response from the model.
- Return type:
- class ChatResponse(*, id=None, message=None, tool_calls=None, usage=None, finish_reason=None)[source]¶
Represents a chat response from the model, based on the provided input.
- Parameters:
id (str | None) – A unique identifier of the response.
message (ChatMessage | None) – The generated message.
tool_calls (List[ChatToolCall] | None) – A list of tool calls.
usage (ChatUsage | None) – Usage statistics.
finish_reason (str | None) – The reason the generation finished. This will be
completedif the generation was successful,max_tokensif the maximum token limit was reached,``content_filter`` if the content filter blocked the response,``tool_calls`` if the model called a tool, orunknownif the reason is unknown.
- class ChatMessage(*, role=None, content=None)[source]¶
The generated chat message.
- Parameters:
role (str | None) – The role of the author of this message.
content (str | None) – The content of the message.
- class ChatUsage(*, input_tokens=None, output_tokens=None, total_tokens=None)[source]¶
Usage statistics for a chat response.
- Parameters:
input_tokens (int | None) – The number of input tokens used.
output_tokens (int | None) – The number of output tokens generated.
total_tokens (int | None) – The total number of tokens used.
- class ChatToolCall(*, id=None, function, type='function')[source]¶
A chat tool call.
- Parameters:
id (str | None) – A unique identifier of the tool call.
function (Function) – The function called.
type (str) – The function type. Always “function”.
- class Function(*, name, arguments)[source]¶
The function called by the model.
- Parameters:
name (str) – The name of the function.
arguments (Dict[str, Any]) – The arguments of the function.
- class EmbeddingResponse(*, id=None, object=None, model=None, usage=None, embeddings)[source]¶
Represents an embedding response from the model, based on the provided input.
- Parameters:
id (str | None) – A unique identifier of the response.
object (str | None) – The object type.
model (str | None) – The model used to generate the response.
usage (EmbeddingUsage | None) – Usage statistics.
embeddings (List[Embedding]) – A list of embeddings.
- class EmbeddingUsage(*, input_tokens=None, total_tokens=None)[source]¶
Usage statistics for an embedding response.
- Parameters:
input_tokens (int | None) – The number of input tokens used.
total_tokens (int | None) – The total number of tokens used.
- class Embedding(*, index, data)[source]¶
An embedding vector representing the input text.
- Parameters:
index (int) – The index of the embedding in the list of embeddings.
data (List[float]) – The embedding vector, which is a list of floats.
- class TranscriptionResponse(*, text)[source]¶
A transcription of an input audio.
- Parameters:
text (str) – The transcribed text.
- class ImageGenerationResponse(*, images)[source]¶
Represents an image generation response from the model, based on the provided input.
- Parameters:
images (List[Any]) – A list of generated images.
SuperClients¶
- class Browser(client)[source]¶
A superclient that extends a chat SwitchAI client to support websites fetching and analysis.
- Parameters:
client (SwitchAI) – A SwitchAI client initialized with a chat model.
- chat(messages, temperature=1.0, max_tokens=None, tools=None, response_format=None, stream=False)[source]¶
Sends a chat request to the AI model and returns the response.
- Parameters:
messages (List[str | dict | ChatResponse]) – A list of messages to send to the model.
temperature (float | None) – Sampling temperature to use, between 0 and 2. Higher values like 0.8 will make
random (the output more)
deterministic. (while lower values like 0.2 will make it more focused and)
max_tokens (int | None) – The maximum number of tokens to generate. Defaults to None.
tools (List | None) – A list of tools the model may call.
response_format (Type[BaseModel] | None) – An object specifying the format that the model must output.
stream (bool | None) – Whether to stream the response.
- Returns:
The response from the model.
- Return type:
- class Classifier(client, classes, task_description=None, multi_label=False)[source]¶
A superclient that extends a chat SwitchAI client to support classification tasks. It can be used to classify text or images.
- Parameters:
client (SwitchAI) – A SwitchAI client initialized with a chat model.
classes (List[str]) – The classes to classify the data into.
task_description (str) – A description of the classification task.
multi_label (bool) – Whether the classifier should support multi-label classification or single-label classification.
- class Illustrator(client)[source]¶
The Illustrator superclient generates illustrations based on text descriptions.
- Parameters:
client (SwitchAI) – A chat SwitchAI client.
- generate_illustration(description, output_path, image_reference=None, max_revision_steps=0, editor_mode=False)[source]¶
Generates an illustration based on the given description and saves it to the specified output path.
- Parameters:
description (str) – The description of the illustration.
output_path (str) – The path where the illustration will be saved. The file format should be SVG.
image_reference (str | bytes | Image) – An image reference to be used to generate the illustration.
max_revision_steps (int) – The maximum number of revision steps allowed to improve the illustration.
0 (If set to)
Otherwise (no revisions will be made.)
the (the model will continue refining)
illustration (illustration until it reaches the maximum number of revision steps or until the)
satisfactory. (is considered)
editor_mode (bool) – If True, allows the user to interactively edit the illustration.
- class ImageRetriever(client, images_folder_path, embeddings_cache_path=None, batch_size=32)[source]¶
A superclient to retrieve images similar to a query image or text.
- Parameters:
client (SwitchAI) – A SwitchAI client that supports text and image embedding.
images_folder_path (str) – The path to the folder containing the images.
embeddings_cache_path (str | None) – The path to the embeddings cache file, else a file named ‘embeddings_cache.json’ will be created in the images folder.
batch_size (int | None) – The batch size to use when embedding images.
- retrieve_images(query, similarity_metric='cosine', threshold=0.5)[source]¶
Retrieve images similar to the query image or text.
- Parameters:
query (str | Image) – The query image or text.
similarity_metric (str) – The similarity metric to use. Must be ‘cosine’ or ‘euclidean’.
threshold (float) – The similarity threshold.
- Returns:
A sorted dictionary containing the image filenames as keys and the similarity scores as values.
- Return type:
Dict[str, float]