brainlid / langchain

Elixir implementation of a LangChain style framework.
https://hexdocs.pm/langchain/
Other
510 stars 58 forks source link

`do_process_response` does not contain handling for all inputs. #20

Closed Cardosaum closed 8 months ago

Cardosaum commented 8 months ago

In chat_open_ai.ex, the function do_process_response can either take a decoded Json, or a {:error, %Jason.DecodeError{}}.

In the definition of the function, we have the matching patterns for properly decoded json, but we lack a definition when the decoding fails.

Example when it fails to match input patterns is reproduced below. It comes from the langchain_demo project (really nice one!)

[debug] HANDLE EVENT "validate" in LangChainDemoWeb.AgentChatLive.Index
  Parameters: %{"_target" => ["chat_message", "content"], "chat_message" => %{"content" => "Hi!"}}
[debug] Replied in 497µs
[debug] HANDLE EVENT "save" in LangChainDemoWeb.AgentChatLive.Index
  Parameters: %{"chat_message" => %{"content" => "Hi!"}}
[debug] Replied in 370µs
[error] Task #PID<0.599.0> started from #PID<0.575.0> terminating
** (FunctionClauseError) no function clause matching in LangChain.ChatModels.ChatOpenAI.do_process_response/1
    (langchain 0.1.1) lib/chat_models/chat_open_ai.ex:413: LangChain.ChatModels.ChatOpenAI.do_process_response({:error, %Jason.DecodeError{position: 0, token: nil, data: <<31, 139, 8, 0, 0, 0, 0, 0, 0, 3, 76, 143, 177, 110, 3, 33, 16, 68, 251, 251, 138, 17, 181, 185, 139, 173, 40, 150, 248, 134, 148, 233, 207, 8, 54, 6, 9, 88, 12, 123, 78, 44, 203, 255, 30, ...>>}})
    (elixir 1.15.2) lib/enum.ex:1693: Enum."-map/2-lists^map/1-1-"/2
    (langchain 0.1.1) lib/chat_models/chat_open_ai.ex:346: LangChain.ChatModels.ChatOpenAI.decode_streamed_data/1
    (langchain 0.1.1) lib/chat_models/chat_open_ai.ex:262: anonymous fn/4 in LangChain.ChatModels.ChatOpenAI.do_api_request/4
    (finch 0.16.0) lib/finch/http1/conn.ex:243: Finch.Conn.receive_response/8
    (finch 0.16.0) lib/finch/http1/conn.ex:120: Finch.Conn.request/6
    (finch 0.16.0) lib/finch/http1/pool.ex:45: anonymous fn/8 in Finch.HTTP1.Pool.request/5
    (nimble_pool 1.0.0) lib/nimble_pool.ex:349: NimblePool.checkout!/4
    (finch 0.16.0) lib/finch/http1/pool.ex:38: Finch.HTTP1.Pool.request/5
    (finch 0.16.0) lib/finch.ex:306: anonymous fn/6 in Finch.stream/5
    (telemetry 1.2.1) /Users/mcs/git/github/brainlid/langchain_demo/deps/telemetry/src/telemetry.erl:321: :telemetry.span/3
    (langchain 0.1.1) lib/chat_models/chat_open_ai.ex:288: anonymous fn/6 in LangChain.ChatModels.ChatOpenAI.do_api_request/4
    (req 0.4.4) lib/req/request.ex:991: Req.Request.run_request/1
    (req 0.4.4) lib/req/request.ex:936: Req.Request.run/1
    (langchain 0.1.1) lib/chat_models/chat_open_ai.ex:318: LangChain.ChatModels.ChatOpenAI.do_api_request/4
    (langchain 0.1.1) lib/chat_models/chat_open_ai.ex:184: LangChain.ChatModels.ChatOpenAI.call/4
    (langchain 0.1.1) lib/chains/llm_chain.ex:204: LangChain.Chains.LLMChain.do_run/1
    (langchain 0.1.1) lib/chains/llm_chain.ex:186: LangChain.Chains.LLMChain.run_while_needs_response/1
    (langchain_demo 0.1.0) lib/langchain_demo_web/live/agent_chat_live/index.ex:315: anonymous fn/2 in LangChainDemoWeb.AgentChatLive.Index.run_chain/1
    (phoenix_live_view 0.20.0) lib/phoenix_live_view/async.ex:77: Phoenix.LiveView.Async.do_async/5
Function: #Function<8.28433447/0 in Phoenix.LiveView.Async.run_async_task/4>
    Args: []