View Source Ollamex.Helpers (Ollamex v0.2.0)

Module containing helper functions used by the other modules of Ollamex.

Summary

Functions

Consolidates a list of %Ollamex.LLMResponse{} structs into a single response, dealing with stream: false and stream: true, regardless of the origin of the responses (/chat or /generate).

Creates an async task from the provided function fun with arguments args.

Because the body of the responses of the /generate, /chat and /embeddings endpoints of the Ollama REST API return different data structures, this helper function detects the endpoint from which a list of responses originated.

Extracts the chat message fragments from a list of %Ollamex.LLMResponse{} from the /chat endpoint and concatenates them into a string.

Extracts the response fragments from a list of %Ollamex.LLMResponse{} from the /generate endpoint and concatenates them into a string.

Converts a map to a target struct, so that API responses can be converted to Ollamex structs.

Yields the result of a task if within the provided timeout (in milliseconds, default value 120_000), or shuts the task down.

Functions

Link to this function

consolidate_responses(r)

View Source (since 0.1.0)

Consolidates a list of %Ollamex.LLMResponse{} structs into a single response, dealing with stream: false and stream: true, regardless of the origin of the responses (/chat or /generate).

Link to this function

create_task(fun, args)

View Source (since 0.1.0)

Creates an async task from the provided function fun with arguments args.

Link to this function

detect_endpoint(r)

View Source (since 0.2.0)

Because the body of the responses of the /generate, /chat and /embeddings endpoints of the Ollama REST API return different data structures, this helper function detects the endpoint from which a list of responses originated.

Link to this function

extract_messages(r)

View Source (since 0.1.0)

Extracts the chat message fragments from a list of %Ollamex.LLMResponse{} from the /chat endpoint and concatenates them into a string.

Link to this function

extract_responses(r)

View Source (since 0.1.0)

Extracts the response fragments from a list of %Ollamex.LLMResponse{} from the /generate endpoint and concatenates them into a string.

Link to this function

handle_response(body)

View Source (since 0.1.0)
Link to this function

map_to_struct(source, target)

View Source (since 0.1.0)

Converts a map to a target struct, so that API responses can be converted to Ollamex structs.

Link to this function

yield_or_timeout_and_shutdown(task, timeout \\ 120_000)

View Source (since 0.1.0)

Yields the result of a task if within the provided timeout (in milliseconds, default value 120_000), or shuts the task down.

Utilized by the Ollamex.generate_with_timeout/2, Ollamex.generate_with_timeout/3, Ollamex.chat_with_timeout/2 and Ollamex.chat_with_timeout/3 functions to deal with cases where the LLM keeps going on and on with repetition.