Skip to content

gen_ai_api

get_embedding_vectors(texts)

Generates embedding vectors for a list of texts using the embedding-specific config.

Parameters:

Name Type Description Default
texts List[str]

List of texts to generate embeddings for.

required

Returns:

Type Description
Dict[str, Any]

A dictionary containing: - "embeddings": A numpy array of embedding vectors. - "usage": A dictionary with token usage and cost details.

Source code in src/ariadne/utils/gen_ai_api.py
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
def get_embedding_vectors(texts: List[str]) -> Dict[str, Any]:
    """
    Generates embedding vectors for a list of texts using the embedding-specific config.

    Args:
        texts: List of texts to generate embeddings for.

    Returns:
        A dictionary containing:
            - "embeddings": A numpy array of embedding vectors.
            - "usage": A dictionary with token usage and cost details.
    """

    client, model, provider = _AIClientFactory.get_client(task_type="embedding")

    response = client.embeddings.create(input=texts, model=model)

    data = sorted(response.data, key=lambda x: x.index)
    np_vectors = np.array([item.embedding for item in data])

    usage = response.usage
    total_cost = _calculate_cost(model, usage.prompt_tokens, 0, provider)

    return {
        "embeddings": np_vectors,
        "usage": {
            "input_tokens": usage.prompt_tokens,
            "output_tokens": 0,
            "reasoning_tokens": 0,
            "total_cost_usd": total_cost,
            "model_used": model,
        },
    }

get_llm_response(prompt, system_prompt=None)

Generates text response using the LLM-specific config.

Parameters:

Name Type Description Default
prompt str

The user prompt to send to the LLM.

required
system_prompt Optional[str]

Optional system prompt to guide the LLM's behavior.

None

Returns: A dictionary containing: - "content": The generated text response from the LLM. - "usage": A dictionary with token usage and cost details.

Source code in src/ariadne/utils/gen_ai_api.py
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
def get_llm_response(prompt: str, system_prompt: Optional[str] = None) -> Dict[str, Any]:
    """
    Generates text response using the LLM-specific config.

    Args:
        prompt: The user prompt to send to the LLM.
        system_prompt: Optional system prompt to guide the LLM's behavior.
    Returns:
        A dictionary containing:
            - "content": The generated text response from the LLM.
            - "usage": A dictionary with token usage and cost details.
    """

    client, model, provider = _AIClientFactory.get_client(task_type="llm")

    messages = []
    if system_prompt:
        messages.append({"role": "system", "content": system_prompt})
    messages.append({"role": "user", "content": prompt})

    if model in _TEMPERATURE_OK_MODELS:
        temperature = 0.0
    else:
        temperature = None

    try:
        response = client.chat.completions.create(model=model, messages=messages, temperature=temperature)
    except Exception as e:
        msg = str(e).lower()
        if any(k in msg for k in ("content filter", "content_filter", "filtered", "policy", "moderation")):
            import warnings

            warnings.warn(f"Content filter triggered for obscene prompt '{prompt}'; returning None as response.")
            return {
                "content": None,
                "usage": {
                    "input_tokens": 0,
                    "output_tokens": 0,
                    "reasoning_tokens": 0,
                    "total_cost_usd": 0.0,
                    "model_used": model,
                },
            }
        raise

    usage = response.usage
    reasoning_tokens = 0
    if hasattr(usage, "completion_tokens_details") and usage.completion_tokens_details:
        reasoning_tokens = getattr(usage.completion_tokens_details, "reasoning_tokens", 0)

    total_cost = _calculate_cost(model, usage.prompt_tokens, usage.completion_tokens, provider)

    return {
        "content": response.choices[0].message.content,
        "usage": {
            "input_tokens": usage.prompt_tokens,
            "output_tokens": usage.completion_tokens,
            "reasoning_tokens": reasoning_tokens,
            "total_cost_usd": total_cost,
            "model_used": model,
        },
    }