-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
501 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
# Used by "mix format" | ||
[ | ||
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] | ||
import_deps: [:ecto, :phoenix, :phoenix_live_view], | ||
plugins: [Phoenix.LiveView.HTMLFormatter], | ||
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}", "pages/cookbook/**/*.{ex,exs}"] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
defmodule Instructor.Adapters.Groq do | ||
@moduledoc """ | ||
Adapter for Groq Cloud API. Using the OpenAI API compatible endpoint. | ||
""" | ||
alias Instructor.Adapters | ||
|
||
@behaviour Instructor.Adapter | ||
@supported_modes [:tools] | ||
|
||
@impl true | ||
def chat_completion(params, user_config \\ nil) do | ||
config = config(user_config) | ||
mode = params[:mode] | ||
|
||
if mode not in @supported_modes do | ||
raise "Unsupported mode #{mode} for Groq" | ||
end | ||
|
||
Adapters.OpenAI.chat_completion(params, config) | ||
end | ||
|
||
@impl true | ||
defdelegate reask_messages(raw_response, params, config), to: Adapters.OpenAI | ||
|
||
defp config(nil), do: config(Application.get_env(:instructor, :groq, [])) | ||
|
||
defp config(base_config) do | ||
default_config = [ | ||
api_url: "https://api.groq.com/openai", | ||
http_options: [receive_timeout: 60_000] | ||
] | ||
|
||
Keyword.merge(default_config, base_config) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
defmodule Instructor.Extras.ChainOfThought do | ||
defmodule ReasoningStep do | ||
use Ecto.Schema | ||
|
||
@doc """ | ||
For each step, provide a title that describes what you're doing in that step, along with the content. | ||
Decide if you need another step or if you're ready to give the final answer. | ||
Respond in JSON format with 'title', 'content', and 'next_action' (either 'continue' or 'final_answer') keys. | ||
""" | ||
@primary_key false | ||
embedded_schema do | ||
field(:title, :string) | ||
field(:content, :string) | ||
field(:next_action, Ecto.Enum, values: [:final_answer, :continue]) | ||
end | ||
end | ||
|
||
def chat_completion(params, config \\ nil) do | ||
reasoning_steps = Keyword.pop(params, :reasoning_steps, 3) | ||
response_model = params[:response_model] | ||
|
||
initial_messages = | ||
[ | ||
%{ | ||
role: "system", | ||
content: """ | ||
You are an expert AI assistant that explains your reasoning step by step. | ||
For each step, provide a title that describes what you're doing in that step, along with the content. | ||
Decide if you need another step or if you're ready to give the final answer. | ||
Respond in JSON format with 'title', 'content', and 'next_action' (either 'continue' or 'final_answer') keys. | ||
USE AS MANY REASONING STEPS AS POSSIBLE. | ||
AT LEAST 3. | ||
# ... (rest of the system message) | ||
""" | ||
} | ||
] ++ | ||
params[:messages] ++ | ||
[ | ||
%{ | ||
role: "assistant", | ||
content: """ | ||
Thank you! I will now think step by step following my instructions, starting at the beginning after decomposing the problem. | ||
""" | ||
} | ||
] | ||
|
||
params = Keyword.put(params, :messages, initial_messages) | ||
params = Keyword.put(params, :response_model, ReasoningStep) | ||
|
||
Stream.resource( | ||
fn -> {params, 0} end, | ||
fn | ||
:halt -> | ||
{:halt, nil} | ||
|
||
{:final_answer, params} -> | ||
new_messages = | ||
params[:messages] ++ | ||
[ | ||
%{ | ||
role: "user", | ||
content: """ | ||
Please provide the final answer based solely on your reasoning above. | ||
Only provide the text response without any titles or preambles. | ||
Retain any formatting as instructed by the original prompt, such as exact formatting for free response or multiple choice. | ||
""" | ||
} | ||
] | ||
|
||
params = Keyword.put(params, :messages, new_messages) | ||
params = Keyword.put(params, :response_model, response_model) | ||
{:ok, final_answer} = Instructor.chat_completion(params, config) | ||
{[{:final_answer, final_answer}], :halt} | ||
|
||
{params, step_count} -> | ||
case Instructor.chat_completion(params, config) do | ||
{:ok, %ReasoningStep{} = step} -> | ||
new_messages = | ||
params[:messages] ++ | ||
[ | ||
%{ | ||
role: "assistant", | ||
content: step |> Map.from_struct() |> Jason.encode!() | ||
} | ||
] | ||
|
||
params = Keyword.put(params, :messages, new_messages) | ||
|
||
acc = | ||
case step.next_action do | ||
:final_answer -> | ||
{:final_answer, params} | ||
|
||
:continue -> | ||
{params, step_count + 1} | ||
end | ||
|
||
{[step], acc} | ||
|
||
{:error, reason} -> | ||
IO.inspect(reason, label: "ERROR") | ||
{:halt, {params, step_count}} | ||
end | ||
end, | ||
fn _ -> nil end | ||
) | ||
|> Stream.transform(0, fn | ||
{:final_answer, final_answer}, _step_count -> | ||
{[final_answer], :halt} | ||
|
||
step, step_count when step_count < reasoning_steps -> | ||
{[step], step_count + 1} | ||
|
||
_step, _step_count -> | ||
{[{:error, "No final answer within #{reasoning_steps} reasoning steps"}], :halt} | ||
end) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.