Skip to content

Commit

Permalink
docs: add example
Browse files Browse the repository at this point in the history
* until this is merged, using my branch for running anthropic examples
  • Loading branch information
TwistingTwists committed Jun 24, 2024
1 parent aa0fc0d commit 2122eea
Showing 1 changed file with 85 additions and 6 deletions.
91 changes: 85 additions & 6 deletions pages/llm-providers/anthropic.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down Expand Up @@ -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,
Expand All @@ -83,6 +81,17 @@ end
is_spam?.("Hello I am a Nigerian prince and I would like to send you money")
````

<!-- livebook:{"output":true} -->

```
{: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.
Expand All @@ -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: [
%{
Expand All @@ -131,4 +141,73 @@ classify_email.("My account is locked and I can't access my billing info.")
[:technical_issue, :billing_issue]
```

<!-- livebook:{"offset":4335,"stamp":{"token":"XCP.z64pnpwAt12QVZZsOwZ6F4ZHjZz_AQ4EOrHm2Oty-LTs40gELuOk8NpLoz4A0d8TU_d_JsWFYjmtedcbLWMIT5fQxG9kUhv7g339Y6UB8ejsS0VXBeBShcuthFzN","version":2}} -->
```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?"}
# ]
# )
```

<!-- livebook:{"output":true} -->

```
{: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()
```

<!-- livebook:{"output":true} -->

```
%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]
}
```

<!-- livebook:{"output":true} -->

```
:ok
```

<!-- livebook:{"offset":5849,"stamp":{"token":"XCP.Kyr0VQqT1yKDP6vCgVHdqb_SnPhqWWna0ANYZulYu-YGE7OwrRJr5U8r6_omoYQocKuofIBpbrGRSxrvvRVfkIPn7AXxBTLsXggxkAQ89NKCjCo3pS_qTTYBH40","version":2}} -->

0 comments on commit 2122eea

Please sign in to comment.