thmsmlr / instructor_ex

Structured outputs for LLMs in Elixir
https://hexdocs.pm/instructor
430 stars 48 forks source link

feat: support for claude #38

Open TwistingTwists opened 3 months ago

TwistingTwists commented 3 months ago

Done so far:

In progress:

TwistingTwists commented 3 months ago

I am taking time off for week or so. Might not be actively able to actively contibute to this PR until 4 april.

Can review changes, comments. :)

TwistingTwists commented 3 months ago

https://github.com/brainlid/langchain/pull/86/files

Reference PR.

TwistingTwists commented 3 months ago

Sax parser for supporting claude native XML format.

https://github.com/qcam/saxy

thbar commented 3 months ago

Examples of using Sax parsing with Saxy (which I used recently), if that's useful!

TwistingTwists commented 3 months ago

Examples of using Sax parsing with Saxy (which I used recently), if that's useful!

Super useful. Gem. 🌸🌸

Will continue working on this pr after holidays finish. :)

TwistingTwists commented 3 months ago

https://twitter.com/jxnlco/status/1773536962403533187

Maybe don't need to do XML stuff. Json mode has been reported to work reliably

noozo commented 2 months ago

{"anthropic-beta", "tools-2024-04-04"} this header is required to use tools now

noozo commented 2 months ago

The function to conform to openai structure should be:

  # %{
  #   "content" => [
  #     %{
  #       "text" => "ACTUAL RESPONSE ",
  #       "type" => "text"
  #     }
  #   ],
  #   "id" => "msg_01W35ZRGGrPaL4fXqgB5fHDs",
  #   "model" => "claude-3-haiku-20240307",
  #   "role" => "assistant",
  #   "stop_reason" => "end_turn",
  #   "stop_sequence" => nil,
  #   "type" => "message",
  #   "usage" => %{"input_tokens" => 243, "output_tokens" => 132}
  # }
  defp to_openai_response(%{"content" => [%{"text" => text}]} = _params) do
    %{
      "choices" => [
        %{
          "message" => %{"content" => text}
        }
      ]
    }
  end
noozo commented 2 months ago

Added PR but removed my fork so i could submit something else to main repo, but it's still here: https://github.com/TwistingTwists/instructor_ex/pull/1/files

TwistingTwists commented 2 months ago

Will work on it over the weekend. Thanks for this @noozo !

TwistingTwists commented 1 week ago

@cigrainger : Streaming response works.

Will add an example in a few minutes. Feel free to checkout this branch.

This might not be mergable, at the moment, because it includes lots of goodies from other branches as well. But it works.

TwistingTwists commented 1 week ago

Big thanks to @noozo , @thbar for the tips ! :)

TwistingTwists commented 1 week ago

Also, since I was testing Gemini as well, I rebased Andres Alejos 's work and put this in the same PR.

can remove this if needed.

TwistingTwists commented 1 week ago

almost done with #33

cigrainger commented 1 week ago

Gave it a try and got the following error:

** (MatchError) no match of right hand side value: {:error, :adapter_error, %{"error" => %{"message" => "tool_choice: Input tag 'function' found using 'type' does not match any of the expected tags: 'auto', 'any', 'tool'", "type" => "invalid_request_error"}, "type" => "error"}}
TwistingTwists commented 6 days ago

@cigrainger ,{:error, error} is always a tuple of two. Also updated the signature of the chat_completion function.