|
| 1 | +<!-- livebook:{"app_settings":{"access_type":"public","show_source":true,"slug":"groq-agent"},"autosave_interval_s":30} --> |
| 2 | + |
| 3 | +# Groq instructor |
| 4 | + |
| 5 | +```elixir |
| 6 | +Mix.install([ |
| 7 | + {:groq, "~> 0.1"}, |
| 8 | + {:hackney, "~> 1.18"}, |
| 9 | + {:jason, "~> 1.4"}, |
| 10 | + {:phoenix_live_view, "~> 0.18.3"}, |
| 11 | + {:kino, "~> 0.9.0"} |
| 12 | +]) |
| 13 | +``` |
| 14 | + |
| 15 | +## Section |
| 16 | + |
| 17 | +```elixir |
| 18 | +Mix.install([ |
| 19 | + {:groq, "~> 0.1"}, |
| 20 | + {:hackney, "~> 1.18"}, |
| 21 | + {:jason, "~> 1.4"}, |
| 22 | + {:phoenix_live_view, "~> 0.18.3"}, |
| 23 | + {:kino, "~> 0.9.0"} |
| 24 | +]) |
| 25 | +``` |
| 26 | + |
| 27 | +```elixir |
| 28 | +json_library = if Code.ensure_loaded?(Jason), do: Jason, else: :error |
| 29 | + |
| 30 | +Application.put_env(:groq, :json_library, json_library) |
| 31 | + |
| 32 | +System.fetch_env!("GROQ_API_KEY") |
| 33 | + |
| 34 | +# Start the Groq application |
| 35 | +case Application.ensure_all_started(:groq) do |
| 36 | + {:ok, _} -> IO.puts("Groq application started successfully") |
| 37 | + {:error, error} -> IO.puts("Failed to start Groq application: #{inspect(error)}") |
| 38 | +end |
| 39 | +``` |
| 40 | + |
| 41 | +```elixir |
| 42 | +Application.ensure_all_started(:hackney) |
| 43 | +Application.ensure_all_started(:groq) |
| 44 | +``` |
| 45 | + |
| 46 | +```elixir |
| 47 | +defmodule MathAgent do |
| 48 | + def calculate(expression) do |
| 49 | + try do |
| 50 | + result = Code.eval_string(expression) |> elem(0) |
| 51 | + Jason.encode!(%{result: result}) |
| 52 | + rescue |
| 53 | + _ -> Jason.encode!(%{error: "Invalid expression"}) |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + def function_properties do |
| 58 | + [ |
| 59 | + %{ |
| 60 | + "type" => "function", |
| 61 | + "function" => %{ |
| 62 | + "name" => "calculate", |
| 63 | + "description" => "Evaluate a mathematical expression", |
| 64 | + "parameters" => %{ |
| 65 | + "type" => "object", |
| 66 | + "properties" => %{ |
| 67 | + "expression" => %{ |
| 68 | + "type" => "string", |
| 69 | + "description" => "The mathematical expression to evaluate" |
| 70 | + } |
| 71 | + }, |
| 72 | + "required" => ["expression"] |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + ] |
| 77 | + end |
| 78 | + |
| 79 | + def create_chat_completion(messages) do |
| 80 | + Groq.ChatCompletion.create(%{ |
| 81 | + "model" => "llama3-groq-70b-8192-tool-use-preview", |
| 82 | + "messages" => messages, |
| 83 | + "tools" => function_properties(), |
| 84 | + "tool_choice" => "auto", |
| 85 | + "max_tokens" => 4096 |
| 86 | + }) |
| 87 | + end |
| 88 | + |
| 89 | + def handle_response({:ok, result}) do |
| 90 | + case result do |
| 91 | + %{"choices" => choices} when is_list(choices) and length(choices) > 0 -> |
| 92 | + first_choice = Enum.at(choices, 0) |
| 93 | + handle_message(first_choice["message"]) |
| 94 | + |
| 95 | + _ -> |
| 96 | + {:error, "Unexpected response structure: #{inspect(result)}"} |
| 97 | + end |
| 98 | + end |
| 99 | + |
| 100 | + def handle_response({:error, error}) do |
| 101 | + {:error, "Error: #{inspect(error)}"} |
| 102 | + end |
| 103 | + |
| 104 | + defp handle_message(%{"tool_calls" => [tool_call | _]} = message) do |
| 105 | + IO.puts("\nModel is using a tool:") |
| 106 | + IO.inspect(message, label: "Full message") |
| 107 | + |
| 108 | + %{"function" => %{"name" => function_name, "arguments" => arguments}} = tool_call |
| 109 | + |
| 110 | + case function_name do |
| 111 | + "calculate" -> |
| 112 | + args = Jason.decode!(arguments) |
| 113 | + IO.puts("Calling calculate function with expression: #{args["expression"]}") |
| 114 | + result = calculate(args["expression"]) |
| 115 | + IO.puts("Calculate function result: #{result}") |
| 116 | + {:tool_call, tool_call["id"], function_name, result} |
| 117 | + |
| 118 | + _ -> |
| 119 | + {:error, "Unknown function: #{function_name}"} |
| 120 | + end |
| 121 | + end |
| 122 | + |
| 123 | + defp handle_message(message) do |
| 124 | + IO.puts("\nModel is not using a tool:") |
| 125 | + IO.inspect(message, label: "Full message") |
| 126 | + {:ok, message["content"]} |
| 127 | + end |
| 128 | + |
| 129 | + def run_conversation(user_prompt) do |
| 130 | + IO.puts("Starting conversation with user prompt: #{user_prompt}") |
| 131 | + |
| 132 | + initial_messages = [ |
| 133 | + %{ |
| 134 | + "role" => "system", |
| 135 | + "content" => "Another fake answer!" |
| 136 | + }, |
| 137 | + %{ |
| 138 | + "role" => "user", |
| 139 | + "content" => user_prompt |
| 140 | + } |
| 141 | + ] |
| 142 | + |
| 143 | + case create_chat_completion(initial_messages) do |
| 144 | + {:ok, result} -> |
| 145 | + IO.puts("\nReceived initial response from Groq API") |
| 146 | + |
| 147 | + case handle_response({:ok, result}) do |
| 148 | + {:tool_call, id, name, content} -> |
| 149 | + IO.puts("\nProcessing tool call result") |
| 150 | + |
| 151 | + tool_message = %{ |
| 152 | + "tool_call_id" => id, |
| 153 | + "role" => "tool", |
| 154 | + "name" => name, |
| 155 | + "content" => content |
| 156 | + } |
| 157 | + |
| 158 | + first_choice = Enum.at(result["choices"], 0) |
| 159 | + new_messages = initial_messages ++ [first_choice["message"], tool_message] |
| 160 | + IO.puts("\nSending follow-up request to Groq API") |
| 161 | + |
| 162 | + case create_chat_completion(new_messages) do |
| 163 | + {:ok, final_result} -> |
| 164 | + IO.puts("\nReceived final response from Groq API") |
| 165 | + handle_response({:ok, final_result}) |
| 166 | + |
| 167 | + error -> |
| 168 | + error |
| 169 | + end |
| 170 | + |
| 171 | + other -> |
| 172 | + other |
| 173 | + end |
| 174 | + |
| 175 | + error -> |
| 176 | + error |
| 177 | + end |
| 178 | + end |
| 179 | +end |
| 180 | +``` |
| 181 | + |
| 182 | +```elixir |
| 183 | +user_input_no_tool = "What's the capital of France?" |
| 184 | + |
| 185 | +IO.puts( |
| 186 | + "\n\nRunning conversation with input that might not use the tool: #{user_input_no_tool}\n" |
| 187 | +) |
| 188 | + |
| 189 | +case MathAgent.run_conversation(user_input_no_tool) do |
| 190 | + {:ok, response} -> IO.puts("\nFinal Response: #{response}") |
| 191 | + {:error, error} -> IO.puts("\nError: #{error}") |
| 192 | +end |
| 193 | +``` |
0 commit comments