OpenaiExPipeline.OpenaiExWrapper (openai_ex_pipeline v0.0.1)

View Source

OpenaiExWrapper functions for API operations.

Summary

Functions

Checks if a string is blank (nil or empty after trimming). Returns true if the string is nil or empty after trimming, false otherwise.

Adds a cache buster parameter to vector store file status requests to prevent caching issues.

Cleans up files, from OpenAI's file storage.

Creates a response using the OpenAI API.

Creates a new vector store with the given name.

Deletes a file from OpenAI's file storage.

Deletes a response from OpenAI.

Removes OpenAI citations from the content. Returns {:ok, filtered_content} or {:error, reason}

Retrieves a file from a vector store.

Extracts the message content from an OpenAI response.

Creates a new OpenAI client with optional configuration.

Lists all files from OpenAI's file storage.

Lists all vector stores from OpenAI.

Logs input messages in a formatted way.

Uploads a file to OpenAI's file storage and optionally connects it to a vector store.

Uploads a file to OpenAI if a valid file path is provided.

Uploads multiple optional files to OpenAI.

Waits for a file to be processed in a vector store.

Functions

blank?(str)

Checks if a string is blank (nil or empty after trimming). Returns true if the string is nil or empty after trimming, false otherwise.

check_status_on_vector_store_file_with_cache_buster(client, file_id, vector_store_id, attempts)

Adds a cache buster parameter to vector store file status requests to prevent caching issues.

This function is used to work around caching problems when recording and replaying requests with ExVCR. By adding a unique cache buster parameter to each request, we ensure that each request is treated as unique and not cached.

Parameters

  • client: The OpenAI client
  • file_id: The ID of the file in the vector store
  • vector_store_id: The ID of the vector store

Returns

  • {:ok, response} on success
  • {:error, reason} on failure

clean_up_files(openai_client, files)

Cleans up files, from OpenAI's file storage.

Parameters

  • openai_client: OpenAI client instance
  • files: List or map of files to clean up
    • If a list, can contain nested lists or file maps
    • If a map, values should be file maps

Returns

  • {:ok} on success
  • {:error, reason} on failure

Examples

iex> files = [%{"id" => "file-123", "filename" => "test.txt"}]
iex> OpenaiExWrapper.clean_up_files(client, files)
{:ok}

connect_file_to_vector_store(openai_client, file_id, vector_store_id)

Connects a file to a vector store.

Parameters

  • openai_client: OpenAI client
  • file_id: ID of the file to connect (can be a map with "id" key or the ID string)
  • vector_store_id: ID of the vector store (can be a map with "id" key or the ID string)

Returns

  • {:ok, response} - Response on successful connection
  • {:error, reason} - Error message on failure

create_response(openai_client, input, params \\ %{})

Creates a response using the OpenAI API.

Parameters

  • openai_client: OpenAI client
  • input: Input for the response
  • params: Additional parameters (default: %{})

Returns

  • {:ok, response, conversation} - Response and conversation on success
  • {:error, reason} - Error message on failure

create_vector_store(openai_client, vector_store_name)

Creates a new vector store with the given name.

Parameters

  • openai_client: OpenAI client
  • vector_store_name: Name of the vector store to create

Returns

  • {:ok, vector_store_res} - Vector store response on success
  • {:error, reason} - Error message on failure

delete_file_from_file_storage(openai_client, file_id)

Deletes a file from OpenAI's file storage.

Parameters

  • client: OpenAI client
  • file: File map containing the file ID to delete

Returns

  • {:ok, response} on success
  • {:error, reason} on failure

delete_response(openai_client, response_id)

Deletes a response from OpenAI.

Parameters

  • openai_client: OpenAI client
  • response_id: ID of the response to delete (can be a map with "id" key or the ID string)

Returns

  • {:ok, "Response deleted"} - Success message on successful deletion
  • {:error, reason} - Error message on failure

delete_vector_store(openai_client, vector_store_id)

Deletes a vector store by ID.

Parameters

  • openai_client: OpenAI client
  • vector_store_id: ID of the vector store to delete (can be a map with "id" key or the ID string)

Returns

  • {:ok, "Vector store deleted"} - Success message on successful deletion
  • {:error, reason} - Error message on failure

filter_content(content)

Removes OpenAI citations from the content. Returns {:ok, filtered_content} or {:error, reason}

get_file_from_vector_store(client, file_id, vector_store_id)

Retrieves a file from a vector store.

Parameters

  • client: OpenAI client
  • file_id: ID of the file (can be a map with "id" key or the ID string)
  • vector_store_id: ID of the vector store (can be a map with "id" key or the ID string)

Returns

  • {:ok, vs_file} - Vector store file on success
  • {:error, reason} - Error message on failure

get_message_from_response(response)

Extracts the message content from an OpenAI response.

Parameters

  • response: OpenAI API response

Returns

  • String containing the message content

get_openai_client(config, openai_client_timeout \\ 90000)

Creates a new OpenAI client with optional configuration.

Parameters

  • config: Map containing OpenAI configuration
    • :api_key - OpenAI API key (required)
    • :organization_key - OpenAI organization key (optional)
    • :project_key - OpenAI project key (optional)
  • openai_client_timeout: Timeout in milliseconds for API requests (defaults to 90,000)

Returns

  • OpenAI client configured with the provided settings

Examples

iex> config = %{api_key: "sk-123", organization_key: "org-123"}
iex> client = OpenaiExWrapper.get_openai_client(config)
iex> client.receive_timeout
90000

list_files(client)

Lists all files from OpenAI's file storage.

Parameters

  • client: OpenAI client

Returns

  • {:ok, files} - List of files on success
  • {:error, reason} - Error message on failure

list_vector_stores(client)

Lists all vector stores from OpenAI.

Parameters

  • client: OpenAI client

Returns

  • {:ok, vector_stores} - List of vector stores on success
  • {:error, reason} - Error message on failure

pretty_log_input(input)

Logs input messages in a formatted way.

Parameters

  • input: List of input messages to log

upload_file(openai_client, file_path, options \\ %{async_connection: false})

Uploads a file to OpenAI's file storage and optionally connects it to a vector store.

Parameters

  • openai_client: OpenAI client
  • file_path: Path to the file to upload
  • options: Optional map of parameters
    • :vector_store_id - If provided, will attempt to connect the uploaded file to the specified vector store after upload
    • :aysnc_connection - if true, will not wait for the vector store connection to complete, defaults to false

Returns

  • {:ok, upload_response} on successful upload (and connection if vector_store_id provided)
  • {:error, reason} if upload or vector store connection fails

upload_optional_file(openai_client, file_path, options \\ %{})

Uploads a file to OpenAI if a valid file path is provided.

Parameters

  • openai_client: The OpenAI client instance
  • file_path: The path to the file to upload
  • options: Additional options for the upload (default: %{})

Returns

  • {:ok, nil} - If file_path is empty, nil, or blank
  • {:ok, upload_response} - If file upload is successful
  • {:error, reason} - If file does not exist or upload fails

Examples

iex> OpenaiExWrapper.upload_optional_file(client, "")
{:ok, nil}

iex> OpenaiExWrapper.upload_optional_file(client, nil)
{:ok, nil}

iex> OpenaiExWrapper.upload_optional_file(client, "path/to/file.md")
{:ok, %{"id" => "file-abc123", ...}}

upload_optional_files(openai_client, file_path, options \\ %{})

Uploads multiple optional files to OpenAI.

Takes a list of file paths and uploads each one. If any upload fails, all previously uploaded files will be automatically deleted to prevent orphaned files.

Returns:

  • {:ok, [uploads]} - List of successful file uploads
  • {:error, reason} - Error message if any upload fails. Previously uploaded files will be deleted.

vector_store_poll_run_status(client, file_id, vector_store_id, attempts \\ 0, sleep_fn \\ &default_sleep_fn/1)

Default sleep function for polling operations.

Parameters

  • ms: Number of milliseconds to sleep

Returns

  • :ok after sleeping for the specified duration

wait_for_vector_store_file_connection(client, file_id, vector_store_id)

Waits for a file to be processed in a vector store.

Parameters

  • client: OpenAI client
  • file_id: ID of the file (can be a map with "id" key or the ID string)
  • vector_store_id: ID of the vector store (can be a map with "id" key or the ID string)

Returns

  • {:ok, vs_file} - Vector store file on successful processing
  • {:error, reason} - Error message on failure