Run this notebook

Use Livebook to open this notebook and explore new ideas.

It is easy to get started, on your machine or the cloud.

Click below to open and run it in your Livebook at .

(or change your Livebook location)

# Agent Prototyping ```elixir Mix.install([ {:magus, path: "#{__DIR__}/.."}, {:kino, "~> 0.12.0"} # {:ex_dot, "~> 0.1.0"} ]) Application.put_env(:magus, :model_provider, "openai") Application.put_env(:magus, :openai_key, System.fetch_env!("LB_OPENAI_KEY")) ``` ## New Agent ```elixir alias LangChain.Message alias Magus.GraphAgent alias Magus.AgentChain write_story_node = fn chain, state -> prompt = "Write a brief story about \"#{state.topic}\" that features three different characters" {:ok, content, _last_message} = chain |> AgentChain.add_message(Message.new_user!(prompt)) |> AgentChain.run() %{state | story: content} end story_schema = %{ "type" => "object", "properties" => %{ "summary" => %{ "type" => "string", "description" => "Brief summary of the story." }, "characters" => %{ "type" => "array", "description" => "List of characters in the story", "items" => %{ "type" => "string" } } } } structure_story_node = fn chain, state -> prompt = """ You are a helpful assistant responsible for summarizing stories and identifying story characters. The following is a short story about "#{state.topic}": #{state.story} """ {:ok, content, _last_message} = chain |> AgentChain.add_message(Message.new_user!(prompt)) |> AgentChain.ask_for_json_response(story_schema) |> AgentChain.run() %{state | structured_story: content} end agent = %GraphAgent{ name: "Poff's Great New Agent", initial_state: %{} } |> GraphAgent.add_node(:write_story, write_story_node) |> GraphAgent.add_node(:structure_story, structure_story_node) |> GraphAgent.set_entry_point(:write_story) |> GraphAgent.add_edge(:write_story, :structure_story) |> GraphAgent.add_edge(:structure_story, :end) ``` <!-- livebook:{"continue_on_error":true} --> ```elixir # This only works if the ex_dot dependency is available (see the Notebook dependencies section) agent.graph |> Graph.Serializers.DOT.serialize() |> case do {:ok, dot_graph} -> dot_graph end |> Dot.to_svg() |> Kino.Image.new(:svg) ``` ```elixir import Kino.Shorts topic = read_text("New story topic: ") if topic == "" do Kino.interrupt!(:error, "You must enter a topic") end initial_state = %{topic: topic, story: nil, structured_story: nil} Magus.AgentExecutorLite.run(%{agent | initial_state: initial_state}) |> Kino.Tree.new() ``` <!-- livebook:{"offset":2428,"stamp":{"token":"XCP.fe-kagN9KEkM7GCeBUphdc8GpDTzYeaHdvtRa0rBLItWPVHrSNZuVhCEUPzxZc6p9pn60VHUHvt1TSP2nuOe61CRl1ZFLWSfH5_NDRbeo8WZZK5vxA","version":2}} -->
See source

Have you already installed Livebook?

If you already installed Livebook, you can configure the default Livebook location where you want to open notebooks.
Livebook up Checking status We can't reach this Livebook (but we saved your preference anyway)
Run notebook

Not yet? Install Livebook in just a minute

Livebook is open source, free, and ready to run anywhere.

Run in the cloud

on select platforms

To run on Linux, Docker, embedded devices, or Elixir’s Mix, check our README.

PLATINUM SPONSORS
SPONSORS
Code navigation with go to definition of modules and functions Read More