OpenaiExPipeline.OpenaiExWrapper (openai_ex_pipeline v0.0.1)
View SourceOpenaiExWrapper 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.
Connects a file to a vector store.
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.
Deletes a vector store by ID.
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.
Default sleep function for polling operations.
Waits for a file to be processed in a vector store.
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.
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
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}
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
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
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
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
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
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
Removes OpenAI citations from the content.
Returns {:ok, filtered_content} or {:error, reason}
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
Extracts the message content from an OpenAI response.
Parameters
- response: OpenAI API response
Returns
- String containing the message content
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
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
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
Logs input messages in a formatted way.
Parameters
- input: List of input messages to log
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
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", ...}}
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.
Default sleep function for polling operations.
Parameters
- ms: Number of milliseconds to sleep
Returns
:okafter sleeping for the specified duration
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