RPA.OpenAI

module RPA.OpenAI

class RPA.OpenAI.OpenAI

Library to support OpenAI and Azure OpenAI services.

Library is not included in the rpaframework package, so in order to use it you have to add rpaframework-openai with the desired version in your conda.yaml file.

Robot Framework example usage

Python example usage

from RPA.Robocorp.Vault import Vault from RPA.OpenAI import OpenAI secrets = Vault().get_secret("OpenAI") baselib = OpenAI() baselib.authorize_to_openai(secrets["key"]) result = baselib.completion_create( Create a tagline for icecream shop', temperature=0.6, ) print(result)

variable ROBOT_LIBRARY_DOC_FORMAT

ROBOT_LIBRARY_DOC_FORMAT = 'REST'

variable ROBOT_LIBRARY_SCOPE

ROBOT_LIBRARY_SCOPE = 'GLOBAL'

method authorize_to_azure_openai

authorize_to_azure_openai(api_key: str, api_base: str, api_type: Optional[str] = 'azure', api_version: Optional[str] = '2023-05-15')

Keyword for authorize to Azure OpenAI.

Parameters
  • api_key – Your Azure OpenAI API key
  • api_base – Your Endpoint URL. Example: https://docs-test-001.openai.azure.com/
  • api_type – β€œazure”
  • api_version – β€œ2023-05-15”

Robot Framework example:

Python example:

secrets = Vault().get_secret("AzureOpenAI") baselib = OpenAI() baselib.authorize_to_azure_openai( secrets["api_key"], secrets["api_base"], "azure", "2023-05-15" )

method authorize_to_openai

authorize_to_openai(api_key: str)

Keyword for authorize to OpenAI with your API key obtained from your account.

  • Parameters: api_key – Your OpenAI API key

Robot Framework example:

Python example:

secrets = Vault().get_secret("OpenAI") baselib = OpenAI() baselib.authorize_to_openai(secrets["key"])

method chat_completion_create

chat_completion_create(user_content: Optional[str] = None, conversation: Optional[List] = None, model: Optional[str] = 'gpt-3.5-turbo', system_content: Optional[str] = None, temperature: Optional[int] = 1, top_probability: Optional[int] = 1, frequency_penalty: Optional[int] = 0, presence_penalty: Optional[int] = 0)

Keyword for creating ChatGPT text completions using OpenAI or Azure OpenAI. Keyword returns the response as a string and the message history as a list.

Note. When using Azure OpenAI you must provide the deployment_name as the model parameter instead of the model ID used with OpenAI.

Parameters
  • user_content – Text submitted to ChatGPT to generate completions.
  • conversation – List containing the conversation to be continued. Leave empty for a new conversation.
  • model – For OpenAI the ID of the model to use, e.g. gpt-4 or gpt-3.5-turbo. For Azure OpenAI the Deployment name, e.g. myGPT4deployment.
  • system_content – The system message helps set the behavior of the assistant.
  • temperature – What sampling temperature to use between 0 to 2. Higher values means the model will take more risks.
  • top_probability – An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass.
  • frequency_penalty – Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far.
  • presence_penalty – Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far.

Robot Framework example:


method completion_create

completion_create(prompt: str, model: Optional[str] = 'text-davinci-003', temperature: Optional[int] = 0.7, max_tokens: Optional[int] = 256, top_probability: Optional[int] = 1, frequency_penalty: Optional[int] = 0, presence_penalty: Optional[int] = 0, result_format: Optional[str] = 'string')

Keyword for creating text completions in OpenAI and Azure OpenAI. Keyword returns a text string.

Note. When using Azure OpenAI you must provide the deployment_name as the model parameter instead of the model ID used with OpenAI.

Parameters
  • prompt – Text submitted to OpenAI for creating natural language.
  • model – For OpenAI the ID of the model to use, e.g. text-davinci-003. For Azure OpenAI the Deployment name, e.g. myDavinci3deployment.
  • temperature – What sampling temperature to use. Higher values means the model will take more risks..
  • max_tokens – The maximum number of tokens to generate in the completion..
  • top_probability – Controls diversity via nucleus sampling. 0.5 means half of all likelihood-weighted options are considered.
  • frequency_penalty – Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far.
  • presence_penalty – Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far.
  • result_format – Result format (string / json). Return just a string or the default JSON response.

Robot Framework example:

Python example:

result = baselib.completion_create( 'Create a tagline for icecream shop', temperature=0.6, ) print(result)

method image_create

image_create(prompt: str, size: Optional[str] = '512x512', num_images: Optional[int] = 1, result_format: Optional[str] = 'list')

Keyword for creating one or more images using OpenAI. Keyword returns a list of urls for the images created.

Note. Keyword not supported in the Azure OpenAI service.

Parameters
  • prompt – A text description of the desired image(s). The maximum length is 1000 characters.
  • size – Size of the files to be created. 256x256, 512x512, 1024x1024
  • num_images – The number of images to generate. Must be between 1 and 10.
  • result_format – Result format (list / json).

Robot Framework example:

Python example:

images = baselib.image_create( 'Cartoon style picture of a cute monkey skateboarding', size='256x256', num_images=2, ) for url in images: print(url)

method image_create_variation

image_create_variation(src_image: str, size: Optional[str] = '512x512', num_images: Optional[int] = 1, result_format: Optional[str] = 'list')

Keyword for creating one or more variations of a image. Keyword returns a list of urls for the images created. Source file must be a valid PNG file, less than 4MB, and square.

Note. Keyword not supported in the Azure OpenAI service.

Parameters
  • src_image – The image to use as the basis for the variation(s). Must be a valid PNG file, less than 4MB, and square.
  • size – The size of the generated images. Must be one of 256x256, 512x512, or 1024x1024.
  • num_images – The number of images to generate. Must be between 1 and 10
  • result_format – Result format (list / json).

Robot Framework example:

Python example:

variations = baselib.image_create_variation( 'source_image.png', size='256x256', num_images=2, ) for url in variations: print(url)