brainlid / langchain

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

I get `{:error, "Unexpected response. {:ok, %LangChain.Chains.LLMChain{ ... }}}` when using the DataExtractionChain #14

Closed ex-nix closed 9 months ago

ex-nix commented 9 months ago

Given the following code in Livebook:

itinerary_1_day = """
Day 1:

Arrive in Delft and check-in at the Hotel De Emauspoort, a cozy boutique hotel located in the heart of the city.
"""

itinerary_day_schema_parameters = %{
  type: "object",
  properties: %{
    destination_name: %{type: "string"},
    destination_type: %{type: "string"}
  },
  required: []
}

# Model setup
{:ok, chat} = LangChain.ChatModels.ChatOpenAI.new(%{model: "gpt-3.5-turbo", temperature: 0, stream: false, verbose: true})

# run the chain on the text information
data_prompt = itinerary_1_day

{:ok, result} = LangChain.Chains.DataExtractionChain.run(chat, itinerary_day_schema_parameters, data_prompt)

I keep getting the following error when running the cell:

** (MatchError) no match of right hand side value: {:error, "Unexpected response. {:ok, %LangChain.Chains.LLMChain{llm: %LangChain.ChatModels.ChatOpenAI{endpoint: \"https://api.openai.com/v1/chat/completions\", model: \"gpt-3.5-turbo\", temperature: 0.0, frequency_penalty: 0.0, receive_timeout: 60000, n: 1, stream: false}, verbose: false, functions: [%LangChain.Function{name: \"information_extraction\", description: \"Extracts the relevant information from the passage.\", function: nil, parameters_schema: %{type: \"object\", required: [\"info\"], properties: %{info: %{type: \"array\", items: %{type: \"object\", required: [], properties: %{destination_name: %{type: \"string\"}, destination_type: %{type: \"string\"}}}}}}}], function_map: %{\"information_extraction\" => %LangChain.Function{name: \"information_extraction\", description: \"Extracts the relevant information from the passage.\", function: nil, parameters_schema: %{type: \"object\", required: [\"info\"], properties: %{info: %{type: \"array\", items: %{type: \"object\", required: [], properties: %{destination_name: %{type: \"string\"}, destination_type: %{type: \"string\"}}}}}}}}, messages: [%LangChain.Message{content: \"You are a helpful assistant that extracts structured data from text passages. Only use the functions you have been provided with.\", index: nil, status: :complete, role: :system, function_name: nil, arguments: nil}, %LangChain.Message{content: \"Extract and save the relevant entities mentioned in the following passage together with their properties.\\n\\n  Passage:\\n  Day 1:\\n\\nArrive in Delft and check-in at the Hotel De Emauspoort, a cozy boutique hotel located in the heart of the city.\\n\", index: nil, status: :complete, role: :user, function_name: nil, arguments: nil}, %LangChain.Message{content: nil, index: 0, status: :complete, role: :assistant, function_name: \"information_extraction\", arguments: %{\"info\" => [%{\"destination_name\" => \"Delft\", \"destination_type\" => \"City\"}, %{\"destination_name\" => \"Hotel De Emauspoort\", \"destination_type\" => \"Hotel\"}]}}], custom_context: nil, delta: nil, last_message: %LangChain.Message{content: nil, index: 0, status: :complete, role: :assistant, function_name: \"information_extraction\", arguments: %{\"info\" => [%{\"destination_name\" => \"Delft\", \"destination_type\" => \"City\"}, %{\"destination_name\" => \"Hotel De Emauspoort\", \"destination_type\" => \"Hotel\"}]}}, needs_response: true, callback_fn: nil}, %LangChain.Message{content: nil, index: 0, status: :complete, role: :assistant, function_name: \"information_extraction\", arguments: %{\"info\" => [%{\"destination_name\" => \"Delft\", \"destination_type\" => \"City\"}, %{\"destination_name\" => \"Hotel De Emauspoort\", \"destination_type\" => \"Hotel\"}]}}}"}
    (stdlib 5.1.1) erl_eval.erl:498: :erl_eval.expr/6
    /home/sylvester/Code/langchain-livebook-examples/embedding-test.livemd#cell:4ashbyu5o6nm3h56cbxgozcrutf33va7:7: (file)

It looks like a valid result, but it's giving me an error anyway. I'm not sure whether I'm doing something wrong or if this is a bug in LangChain.

amokan commented 9 months ago

@sylvester-roos yep. confirmed this doesn't work for me either. Seems to be a bad pattern match.

brainlid commented 9 months ago

@sylvester-roos and @amokan, correct! I changed the way function_calls worked internally and didn't re-run that test (because it's a "live" test). Oops. Thanks for the fix! Merged.

❤️💛💙💜