Skip to content

Commit

Permalink
Handle case where message from LLM has no content
Browse files Browse the repository at this point in the history
  • Loading branch information
jwilger committed Feb 18, 2024
1 parent 7d77329 commit 431efc8
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions lib/gpt_agent.ex
Original file line number Diff line number Diff line change
Expand Up @@ -175,30 +175,36 @@ defmodule GptAgent do
|> noreply()
end

defp process_messages(%__MODULE__{} = state, messages) do
log("Processing messages: #{inspect(messages)}")

Enum.reduce(messages, state, fn message, state ->
[%{"text" => %{"value" => content}} | _rest] = message["content"]

if message["role"] == "assistant" do
publish_event(
state,
AssistantMessageAdded.new!(
message_id: message["id"],
thread_id: message["thread_id"],
run_id: message["run_id"],
assistant_id: message["assistant_id"],
content: content
defp process_message(message, %__MODULE__{} = state) do
case message["content"] do
[%{"text" => %{"value" => content}} | _rest] ->
if message["role"] == "assistant" do
publish_event(
state,
AssistantMessageAdded.new!(
message_id: message["id"],
thread_id: message["thread_id"],
run_id: message["run_id"],
assistant_id: message["assistant_id"],
content: content
)
)
)

log("Updating last message ID to #{message["id"]}")
%{state | last_message_id: message["id"]}
else
log("Updating last message ID to #{message["id"]}")
%{state | last_message_id: message["id"]}
else
state
end

_ ->
log("Skipping message with no content: #{inspect(message)}")
state
end
end)
end
end

defp process_messages(%__MODULE__{} = state, messages) do
log("Processing messages: #{inspect(messages)}")
Enum.reduce(messages, state, &process_message/2)
end

defp heartbeat_interval_ms, do: Application.get_env(:gpt_agent, :heartbeat_interval_ms, 1000)
Expand Down

0 comments on commit 431efc8

Please sign in to comment.