Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ v3.8.3

*Release date: In development*

- Fix `openai_request` to rename `azure_api_key` and `openai_api_key` parameters to `api_key` to avoid errors when Azure and OpenAI are created.
- Add `openai_temperature` and `azure_temperature` parameters to be configurable in `toolium.cfg` file to set the temperature for OpenAI and Azure requests.
- Generalize documentation for `openai_request` configuration.

v3.8.2
------

Expand Down
123 changes: 48 additions & 75 deletions docs/ai_utils.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,54 @@
AI Utils
========

Installation and configuration
------------------------------

Make sure to install the required libraries for the chosen method. For SpaCy, Sentence Transformers or OpenAI LLM, you
can install them with the following command:

.. code-block:: bash

pip install toolium[ai]

**Additional Requirements:**

For SpaCy, you also need to download the language model, i.e. for small English model:

.. code-block:: bash

python -m spacy download en_core_web_sm

For OpenAI LLM, you need to set up your configuration using environment variables or include it in the Toolium configuration.
The required parameters will vary depending on your access type (direct OpenAI or Azure OpenAI):

.. code-block:: bash

# For example, to configure direct OpenAI access:
OPENAI_API_KEY=<your_api_key>

.. code-block:: bash

# For example, to configure Azure OpenAI:
AZURE_OPENAI_API_KEY=<your_api_key>
AZURE_OPENAI_ENDPOINT=<your_endpoint>
OPENAI_API_VERSION=<your_api_version>

*[AI]* section::

[AI]
# If you are using Azure OpenAI
azure_api_key: your-azure-api-key
azure_endpoint: https://your-endpoint.azure.com
api_version: 2025-01-01-preview
azure_deployment: gpt-4o-mini
azure_temperature: 0.7 # Optional Temperature for OpenAI model, between 0 and 1.
# If you are using OpenAI directly
openai_api_key: your-openai-api-key
Comment thread
rgonalo marked this conversation as resolved.
openai_temperature: 0.7 # Optional Temperature for OpenAI model, between 0 and 1.

Different AI providers can be used in the same project.

Text Similarity
---------------

Expand Down Expand Up @@ -58,48 +106,6 @@ To select models for each method, you can refer to the following links:
* `Sentence Transformers models <https://github.com/UKPLab/sentence-transformers>`_
* `OpenAI models <https://platform.openai.com/docs/models>`_

Installation
~~~~~~~~~~~~

Make sure to install the required libraries for the chosen method. For SpaCy, Sentence Transformers or OpenAI LLM, you
can install them with the following command:

.. code-block:: bash

pip install toolium[ai]

**Additional Requirements:**

For SpaCy, you also need to download the language model, i.e. for small English model:

.. code-block:: bash

python -m spacy download en_core_web_sm

For OpenAI LLM, you need to set up your configuration using environment variables or include it in the Toolium configuration.
The required parameters will vary depending on your access type (direct OpenAI or Azure OpenAI):

.. code-block:: bash

# For example, to configure direct OpenAI access:
OPENAI_API_KEY=<your_api_key>

.. code-block:: bash

# For example, to configure Azure OpenAI:
AZURE_OPENAI_API_KEY=<your_api_key>
AZURE_OPENAI_ENDPOINT=<your_endpoint>
OPENAI_API_VERSION=<your_api_version>

*[AI]* section::

[AI]
text_similarity_method: azure_openai
azure_endpoint: https://your-endpoint.azure.com
api_version: 2025-01-01-preview
azure_deployment: gpt-4o-mini


Text Criteria Analysis
----------------------

Expand Down Expand Up @@ -162,7 +168,6 @@ Default text analysis method can be set in the properties.cfg file with the prop
[AI]
text_analysis_method: openai # Options: 'openai', 'azure_openai' (default: azure_openai)


Response Format
~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -236,13 +241,6 @@ For more information on SpaCy models, you can refer to the following link:

* `SpaCy models <https://spacy.io/models>`_

Installation
~~~~~~~~~~~~

The requirements are the same explained for `SpaCy` in the
`installation section of Text Similarity <https://toolium.readthedocs.io/en/latest/ai_utils.html#installation>`_


Answer Evaluation using LLM-as-a-Judge
--------------------------------------

Expand Down Expand Up @@ -383,31 +381,6 @@ Default OpenAI model can be set in the properties.cfg file in the *[AI]* section

[AI]
provider: azure # AI provider to use, openai by default
openai_model: gpt-4o # OpenAI model to use, gpt-4o-mini by default

Installation
~~~~~~~~~~~~

Make sure to install the required libraries:

.. code-block:: bash

pip install toolium[ai]

For Azure OpenAI, you need to set up your configuration in environment variables:

.. code-block:: bash

AZURE_OPENAI_API_KEY=<your_api_key>
AZURE_OPENAI_ENDPOINT=<your_endpoint>
OPENAI_API_VERSION=<your_api_version>

For standard OpenAI:

.. code-block:: bash

OPENAI_API_KEY=<your_api_key>


.. _accuracy_tags_for_behave_scenarios:

Expand Down
18 changes: 9 additions & 9 deletions toolium/utils/ai_utils/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,23 @@ def openai_request(system_message, user_message, model_name=None, azure=False, *
config = DriverWrappersPool.get_default_wrapper().config
model_name = model_name or config.get_optional('AI', 'openai_model', 'gpt-4o-mini')
logger.info('Calling to OpenAI API with model %s', model_name)

response_format = None
if kwargs.get('response_format'):
response_format = kwargs.pop('response_format')
kwargs.pop('response_format', None)
client = AzureOpenAI(**kwargs) if azure else OpenAI(**kwargs)
if azure:
for key in ('azure_api_key', 'azure_endpoint', 'api_version', 'azure_deployment'):
value = config.get_optional('AI', key)
if value:
kwargs.setdefault(key, value)
kwargs_key = 'api_key' if key == 'azure_api_key' else key
Comment thread
rgonalo marked this conversation as resolved.
kwargs.setdefault(kwargs_key, value)
temperature = config.get_optional('AI', 'azure_temperature', None)
else:
for key in ('openai_api_key', 'openai_temperature'):
value = config.get_optional('AI', key)
if value:
kwargs.setdefault(key, value)
kwargs.setdefault('api_key', config.get_optional('AI', 'openai_api_key'))
temperature = config.get_optional('AI', 'openai_temperature', None)
client = AzureOpenAI(**kwargs) if azure else OpenAI(**kwargs)
messages = []
temperature = float(temperature) if temperature is not None else None
if isinstance(system_message, list):
for prompt in system_message:
messages.append({'role': 'system', 'content': prompt})
Expand All @@ -71,11 +71,11 @@ def openai_request(system_message, user_message, model_name=None, azure=False, *

if response_format:
completion = client.beta.chat.completions.parse(
model=model_name, messages=messages, response_format=response_format
model=model_name, messages=messages, response_format=response_format, temperature=temperature
)
response = completion.choices[0].message.parsed
else:
completion = client.chat.completions.create(model=model_name, messages=messages)
completion = client.chat.completions.create(model=model_name, messages=messages, temperature=temperature)
response = completion.choices[0].message.content
token_usage = {}
if completion.usage:
Expand Down