You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-**New Adapters**: Anthropic, Gemini, Groq, Ollama, and VLLM. Each of these provides specialized support for their respective LLM APIs.
7
+
-**`:json_schema` Mode**: The OpenAI adapter and others now support a `:json_schema` mode for more structured JSON outputs.
8
+
-**`Instructor.Extras.ChainOfThought`**: A new module to guide multi-step reasoning processes with partial returns and final answers.
9
+
-**Enhanced Streaming**: More robust partial/array streaming pipelines, plus improved SSE-based parsing for streamed responses.
10
+
-**Re-ask/Follow-up Logic**: Adapters can now handle re-asking the LLM to correct invalid JSON responses when `max_retries` is set.
11
+
12
+
### Changed
13
+
-**OpenAI Adapter Refactor**: A major internal refactor for more flexible streaming modes, additional “response format” options, and better error handling.
14
+
-**Ecto Dependency**: Updated from `3.11` to `3.12`.
15
+
-**Req Dependency**: Now supports `~> 0.5` or `~> 1.0`.
16
+
17
+
### Deprecated
18
+
-**Schema Documentation via `@doc`**: Schemas using `@doc` to send instructions to the LLM will now emit a warning. Please migrate to `@llm_doc` via `use Instructor`.
19
+
20
+
### Breaking Changes
21
+
- Some adapter configurations now require specifying an `:api_path` or `:auth_mode`. Verify your adapter config matches the new format.
22
+
- The OpenAI adapter’s `:json_schema` mode strips unsupported fields (e.g., `format`, `pattern`) from schemas before sending them to the LLM.
23
+
24
+
### Fixed
25
+
- Various improvements to JSON parsing and streaming handling, including better handling of partial/invalid responses.
Structured prompting for LLMs. Instructor is a spiritual port of the great [Instructor Python Library](https://github.com/jxnl/instructor) by [@jxnlco](https://twitter.com/jxnlco), check out his [talk on YouTube](https://www.youtube.com/watch?v=yj-wSRJwrrc).
17
-
18
-
The Instructor library is useful for coaxing an LLM to return JSON that maps to an Ecto schema that you provide, rather than the default unstructured text output. If you define your own validation logic, Instructor can automatically retry prompts when validation fails (returning natural language error messages to the LLM, to guide it when making corrections).
16
+
Check out our [Quickstart Guide](https://hexdocs.pm/instructor/quickstart.html) to get up and running with Instructor in minutes.
19
17
20
-
Instructor is designed to be used with the [OpenAI API](https://platform.openai.com/docs/api-reference/chat-completions/create) by default, but it also works with [llama.cpp](https://github.com/ggerganov/llama.cpp) and [Bumblebee](https://github.com/elixir-nx/bumblebee) (Coming Soon!) by using an extendable adapter behavior.
18
+
Instructor provides structured prompting for LLMs. It is a spiritual port of the great [Instructor Python Library](https://github.com/jxnl/instructor) by [@jxnlco](https://twitter.com/jxnlco).
19
+
20
+
Instructor allows you to get structured output out of an LLM using Ecto.
21
+
You don't have to define any JSON schemas.
22
+
You can just use Ecto as you've always used it.
23
+
And since it's just ecto, you can provide change set validations that you can use to ensure that what you're getting back from the LLM is not only properly structured, but semantically correct.
24
+
25
+
To learn more about the philosophy behind Instructor and its motivations, check out this Elixir Denver Meetup talk:
26
+
27
+
<divstyle="text-align: center">
28
+
29
+
[](https://www.youtube.com/watch?v=RABXu7zqnT0)
30
+
31
+
</div>
32
+
33
+
While Instructor is designed to be used with OpenAI, it also supports every major AI lab and open source LLM inference server:
34
+
35
+
- OpenAI
36
+
- Anthropic
37
+
- Groq
38
+
- Ollama
39
+
- Gemini
40
+
- vLLM
41
+
- llama.cpp
21
42
22
43
At its simplest, usage is pretty straightforward:
23
44
24
-
1. Create an ecto schema, with a `@doc` string that explains the schema definition to the LLM.
25
-
2. Define a `validate_changeset/1` function on the schema, and use the `Instructor.Validator` macro in order for Instructor to know about it.
45
+
1. Create an ecto schema, with a `@llm_doc` string that explains the schema definition to the LLM.
46
+
2. Define a `validate_changeset/1` function on the schema, and use the `use Instructor` macro in order for Instructor to know about it.
26
47
2. Make a call to `Instructor.chat_completion/1` with an instruction for the LLM to execute.
27
48
28
49
You can use the `max_retries` parameter to automatically, iteratively go back and forth with the LLM to try fixing validation errorswhen they occur.
29
50
30
51
```elixir
52
+
Mix.install([:instructor])
53
+
31
54
defmoduleSpamPredictiondo
32
55
useEcto.Schema
33
-
useInstructor.Validator
56
+
useValidator
34
57
35
-
@doc """
58
+
@llm_doc"""
36
59
## Field Descriptions:
37
60
- class: Whether or not the email is spam.
38
61
- reason: A short, less than 10 word rationalization for the classification.
@@ -57,7 +80,7 @@ end
57
80
58
81
is_spam? =fn text ->
59
82
Instructor.chat_completion(
60
-
model:"gpt-3.5-turbo",
83
+
model:"gpt-4o-mini",
61
84
response_model:SpamPrediction,
62
85
max_retries:3,
63
86
messages: [
@@ -69,9 +92,10 @@ is_spam? = fn text ->
69
92
They sell all types of clothing.
70
93
71
94
Classify the following email:
72
-
```
73
-
#{text}
74
-
```
95
+
96
+
<email>
97
+
#{text}
98
+
</email>
75
99
"""
76
100
}
77
101
]
@@ -83,17 +107,6 @@ is_spam?.("Hello I am a Nigerian prince and I would like to send you money")
Check out our [Quickstart Guide](https://hexdocs.pm/instructor/quickstart.html) for more code snippets that you can run locally (in Livebook). Or, to get a better idea of the thinking behind Instructor, read more about our [Philosophy & Motivations](https://hexdocs.pm/instructor/philosophy.html).
87
-
88
-
Optionally, you can also customize the your llama.cpp calls (with defaults shown):
InstructorEx uses [Code.fetch_docs/1](https://hexdocs.pm/elixir/1.16.2/Code.html#fetch_docs/1) to fetch LLM instructions from the Ecto schema specified in `response_model`. If your project is deployed using [releases](https://hexdocs.pm/mix/Mix.Tasks.Release.html), add the following configuration to mix.exs to prevent docs from being stripped from the release:
112
-
113
-
```elixir
114
-
defprojectdo
115
-
# ...
116
-
releases: [
117
-
myapp: [
118
-
strip_beams: [keep: ["Docs"]]
119
-
]
119
+
{:instructor, "~> 0.1.0"}
120
120
]
121
121
end
122
-
```
123
-
124
-
## TODO
125
-
126
-
-[ ] Partial Schemaless doesn't work since fields are set to required in Ecto.
127
-
-[x] Groq adapter
128
-
-[ ]@doc gets stripped in release, find a workaround
129
-
-[ ] ChainOfThought doesn't work with max_retries
130
-
-[ ] Logging for Distillation / Finetuning
131
-
-[ ] Add a Bumblebee adapter
132
-
-[ ] Support naked ecto types by auto-wrapping, not just maps of ecto types, do not wrap if we don't need to... Current codepaths are muddled
133
-
-[ ] Optional/Maybe types
134
-
-[ ] Add Livebook Tutorials, include in Hexdocs
135
-
-[x] Text Classification
136
-
-[ ] Self Critique
137
-
-[ ] Image Extracting Tables
138
-
-[ ] Moderation
139
-
-[x] Citations
140
-
-[ ] Knowledge Graph
141
-
-[ ] Entity Resolution
142
-
-[ ] Search Queries
143
-
-[ ] Query Decomposition
144
-
-[ ] Recursive Schemas
145
-
-[x] Table Extraction
146
-
-[x] Action Item and Dependency Mapping
147
-
-[ ] Multi-File Code Generation
148
-
-[ ] PII Data Sanitizatiommersed
149
-
-[x] Update hexdocs homepage to include example for tutorial
150
-
151
-
## Blog Posts
152
-
153
-
-[ ] Why structured prompting?
154
-
155
-
Meditations on new HCI.
156
-
Finally we have software that can understand text. f(text) -> text.
157
-
This is great, as it gives us a new domain, but the range is still text.
158
-
While we can use string interpolation to map Software 1.0 into f(text), the outputs are not interoperable with Software 1.0.
159
-
Hence why UXs available to us are things like Chatbots as our users have to interpret the output.
160
-
161
-
Instructor, structure prompting, gives use f(text) -> ecto_schema.
162
-
Schemas are the lingua franca of Software 1.0.
163
-
With Instrutor we can now seamlessly move back and forth between Software 1.0 and Software 2.0.
164
-
165
-
Now we can maximally leverage AI...
166
-
167
-
-[ ] From GPT-4 to zero-cost production - Distilation, local-llms, and the cost structure of AI.
0 commit comments