From 2122eeac526cd9ed52c24be518f1bf12d4232ed6 Mon Sep 17 00:00:00 2001 From: Abhishek Tripathi Date: Mon, 24 Jun 2024 15:19:09 +0530 Subject: [PATCH] docs: add example * until this is merged, using my branch for running anthropic examples --- pages/llm-providers/anthropic.livemd | 91 ++++++++++++++++++++++++++-- 1 file changed, 85 insertions(+), 6 deletions(-) diff --git a/pages/llm-providers/anthropic.livemd b/pages/llm-providers/anthropic.livemd index 5e09180..e8e2ada 100644 --- a/pages/llm-providers/anthropic.livemd +++ b/pages/llm-providers/anthropic.livemd @@ -5,9 +5,7 @@ ```elixir Mix.install( [ - {:instructor, - path: - Path.expand("/Users/abhishek/Downloads/perps/experiments/open_source_elixir/instructor_ex")} + {:instructor, git: "https://github.com/TwistingTwists/instructor_ex", branch: "claude_json"} ], config: [ instructor: [ @@ -58,7 +56,7 @@ end is_spam? = fn text -> Instructor.chat_completion( - model: "claude-3-haiku-20240307", + model: "claude-3-5-sonnet-20240620", response_model: SpamPrediction, max_retries: 3, mode: :md_json, @@ -83,6 +81,17 @@ end is_spam?.("Hello I am a Nigerian prince and I would like to send you money") ```` + + +``` +{:ok, + %SpamPrediction{ + class: :spam, + reason: "Nigerian prince scam, unrelated to clothing retail", + score: 0.99 + }} +``` + We don't have to stop just at a boolean inclusion, we can also easily extend this idea to multiple categories or classes that we can classify the text into. In this example, let's consider classifying support emails. We want to know whether it's a `general_inquiry`, `billing_issue`, or a `technical_issue` perhaps it rightly fits in multiple classes. This can be useful if we want to cc' specialized support agents when intersecting customer issues occur We can leverage `Ecto.Enum` to define a schema that restricts the LLM output to be a list of those values. We can also provide a `@doc` description to help guide the LLM with the semantic understanding of what these classifications ought to represent. @@ -109,7 +118,8 @@ end classify_email = fn text -> {:ok, %{tags: result}} = Instructor.chat_completion( - model: "gpt-3.5-turbo", + mode: :md_json, + model: "claude-3-5-sonnet-20240620", response_model: EmailClassifications, messages: [ %{ @@ -131,4 +141,73 @@ classify_email.("My account is locked and I can't access my billing info.") [:technical_issue, :billing_issue] ``` - +```elixir +defmodule President do + use Ecto.Schema + + @primary_key false + embedded_schema do + field(:first_name, :string) + field(:last_name, :string) + field(:entered_office_date, :date) + end +end + +# Instructor.chat_completion( +# mode: :json, +# model: "claude-3-5-sonnet-20240620", +# response_model: President, +# messages: [ +# %{role: "user", content: "Who was the first president of the United States?"} +# ] +# ) +``` + + + +``` +{:module, President, <<70, 79, 82, 49, 0, 0, 15, ...>>, + [__schema__: 1, __schema__: 1, __schema__: 1, __schema__: 1, __schema__: 2, __schema__: 2, ...]} +``` + +```elixir +Instructor.chat_completion( + model: "claude-3-5-sonnet-20240620", + mode: :md_json, + stream: true, + response_model: {:array, President}, + messages: [ + %{role: "user", content: "Who are the first three presidents"} + ] +) +|> Stream.each(fn {:ok, x} -> IO.inspect(x) end) +|> Stream.run() +``` + + + +``` +%President{ + first_name: "George", + last_name: "Washington", + entered_office_date: ~D[1789-04-30] +} +%President{ + first_name: "John", + last_name: "Adams", + entered_office_date: ~D[1797-03-04] +} +%President{ + first_name: "Thomas", + last_name: "Jefferson", + entered_office_date: ~D[1801-03-04] +} +``` + + + +``` +:ok +``` + +