instructor_ex icon indicating copy to clipboard operation
instructor_ex copied to clipboard

feat: support for claude

Open TwistingTwists opened this issue 1 year ago • 20 comments

Done so far:

  • [x] basic md_json mode support
  • [x] basic text support
  • [x] do_streaming_chat_completion/2 in anthropic.ex
  • [x] to_openai_response/1 needs to be more robust

In progress:

  • [ ] vision support
  • [ ] tool mode for anthropic https://docs.anthropic.com/claude/docs/functions-external-tools#function-calling-format = handle formatting request and parsing response *

TwistingTwists avatar Mar 25 '24 04:03 TwistingTwists

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 avatar Mar 26 '24 11:03 TwistingTwists

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

Reference PR.

TwistingTwists avatar Mar 26 '24 22:03 TwistingTwists

Sax parser for supporting claude native XML format.

https://github.com/qcam/saxy

TwistingTwists avatar Mar 28 '24 14:03 TwistingTwists

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

  • https://hexdocs.pm/saxy/getting-started-with-sax.html
  • https://github.com/etalab/transport-site/pull/3836

thbar avatar Mar 28 '24 15:03 thbar

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 avatar Mar 29 '24 00:03 TwistingTwists

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

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

TwistingTwists avatar Mar 29 '24 12:03 TwistingTwists

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

noozo avatar Apr 05 '24 11:04 noozo

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 avatar Apr 05 '24 13:04 noozo

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

noozo avatar Apr 05 '24 14:04 noozo

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

TwistingTwists avatar Apr 06 '24 05:04 TwistingTwists

@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 avatar Jun 24 '24 09:06 TwistingTwists

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

TwistingTwists avatar Jun 24 '24 09:06 TwistingTwists

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 avatar Jun 24 '24 09:06 TwistingTwists

almost done with #33

TwistingTwists avatar Jun 24 '24 16:06 TwistingTwists

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"}}

cigrainger avatar Jun 26 '24 11:06 cigrainger

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

TwistingTwists avatar Jun 27 '24 12:06 TwistingTwists

eagerly waiting for this 🎉

ice-cap0 avatar Jul 09 '24 11:07 ice-cap0

@frozencap : feel free to try it out today from git directly. Open to feedback.

{:instructor_ex, git: "https://github.com/TwistingTwists/instructor_ex/", branch: "claude_json"}

TwistingTwists avatar Jul 10 '24 10:07 TwistingTwists

breaks with ecto 3.12

southkeys avatar Sep 02 '24 17:09 southkeys

breaks with ecto 3.12

Thanks for reporting. will do it over weekend

TwistingTwists avatar Sep 06 '24 01:09 TwistingTwists