View Source HyperLLM.Chat (hyper_llm v0.0.1)

HypperLLM.Chat is a single interface for interacting with LLM providers. The interface uses the OpenAI chat completion API. https://platform.openai.com/docs/api-reference/chat

Example

A Liveview that sends messages to the chat and updates the chat with the response.

defmodule ChatLive do
  use Phoenix.LiveView

  def mount(params, session, socket) do
    {:ok,
    socket
    |> assign(chat: HyperLLM.Chat.start(model: "gpt-4o-mini"))}
  end

  def handle_event("send_message", %{"message" => message}, socket) do
    chat = HyperLLM.Chat.append(socket.assigns.chat, message)

    send(self(), :chat_completion)

    {:noreply, socket |> assign(chat: chat)}
  end

  def handle_info(:chat_completion, socket) do
    with {:ok, response} <- HyperLLM.Chat.completion(socket.assigns.chat) do
      chat = HyperLLM.Chat.append(socket.assigns.chat, response)
      {:noreply, socket |> assign(chat: chat)}
    end
  end
end

Summary

Types

config()

@type config() :: [Keyword.t()]

t()

@type t() :: %HyperLLM.Chat{config: term(), messages: term(), provider: term()}

Functions

append(chat, messages)

@spec append(t(), [HyperLLM.Chat.Message.t()]) :: t()
@spec append(t(), HyperLLM.Chat.Message.t()) :: t()

Append a message to the chat.

Examples

iex> chat = HyperLLM.Chat.start(model: "gpt-4o-mini")
iex> HyperLLM.Chat.append(chat, ["Hello", "World"])
%HyperLLM.Chat{messages: [%HyperLLM.Chat.Message{role: :user, content: "Hello"}, %HyperLLM.Chat.Message{role: :user, content: "World"}], provider: HyperLLM.Provider.OpenAI, config: [model: "gpt-4o-mini"]}

iex> chat = HyperLLM.Chat.start(model: "gpt-4o-mini")
iex> HyperLLM.Chat.append(chat, "Hello")
%HyperLLM.Chat{messages: [%HyperLLM.Chat.Message{role: :user, content: "Hello"}], provider: HyperLLM.Provider.OpenAI, config: [model: "gpt-4o-mini"]}

append(chat, role, content)

@spec append(t(), atom(), binary()) :: t()

completion(chat, config \\ [])

@spec completion(t(), config()) :: binary()

start(config \\ [])

@spec start(config()) :: t()

Start a new chat.

Examples

iex> HyperLLM.Chat.start(model: "gpt-4o-mini") %HyperLLM.Chat{messages: [], provider: HyperLLM.Provider.OpenAI, config: [model: "gpt-4o-mini"]}