Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner as a feature #1649

Merged
merged 4 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions example/agents/customer_support/config.edn
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
:dbname #$ [PGVECTOR_DB_NAME "postgres"]
:user #$ [PGVECTOR_DB_USERNAME "postgres"]
:password #$ [PGVECTOR_DB_PASSWORD "postgres"]}}
:agents {:technical-support {:Documents ["file://./docs/abc.md" "file://./docs/xyz.md"]}
:price-enquiry {:Documents ["file://./docs/abc_prices.txt" "file://./docs/xyz_prices.txt"]}}}
:agents {:Customer.Support.Core/TechnicalHelpAgent {:Documents ["file://./docs/abc.md" "file://./docs/xyz.md"]}
:Customer.Support.Core/PriceLookupAgent {:Documents ["file://./docs/abc_prices.txt" "file://./docs/xyz_prices.txt"]}}}
27 changes: 14 additions & 13 deletions example/agents/customer_support/customer/support/core.al
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
:UserInstruction :String})

{:Agentlang.Core/Agent
{:Name :technical-support
{:Name :Customer.Support.Core/TechnicalHelpAgent
:LLM :llm01
:Input :Customer.Support.Core/CallTechnicalHelp
:UserInstruction
Expand All @@ -35,7 +35,7 @@
:UserInstruction :String})

{:Agentlang.Core/Agent
{:Name :price-enquiry
{:Name :Customer.Support.Core/PriceLookupAgent
:LLM :llm01
:Input :Customer.Support.Core/CallPriceLookup
:UserInstruction
Expand All @@ -50,32 +50,33 @@
;; :Documents
;; [{:Title "ABC Price List"
;; :Uri "file://./docs/abc_prices.txt"
;; :Agent price-enquiry-agent}
;; :Agent :Customer.Support.Core/PriceLookupAgent}
;; {:Title "XYZ Price List"
;; :Uri "file://./docs/xyz_prices.txt"
;; :Agent price-enquiry-agent}]}
;; :Agent :Customer.Support.Core/PriceLookupAgent}]}

(event
:ClassifyRequest
{:meta {:doc "Returns a text, either \"technical-support\" or \"price-enquiry\""}
:UserInstruction :String})

{:Agentlang.Core/Agent
{:Name :request-classifier-agent
{:Name :Customer.Support.Core/ClassifyRequestAgent
:LLM :llm01
:UserInstruction "Classify the request as either one of \"technical-support\" or \"price-enquiry\"."
:UserInstruction (str "Classify the request as either one of \"technical-support\" or \"price-enquiry\". "
"Only return either \"technical-support\" or \"price-enquiry\" and nothing else.\n")
:Input :Customer.Support.Core/ClassifyRequest}}

{:Agentlang.Core/Agent
{:Name :camera-support-agent
:Type :planner
{:Name :Customer.Support.Core/CameraSupportAgent
:LLM :llm01
:Delegates [:request-classifier-agent :technical-support :price-enquiry]
:Delegates [:Customer.Support.Core/ClassifyRequestAgent
:Customer.Support.Core/TechnicalHelpAgent
:Customer.Support.Core/PriceLookupAgent]
:UserInstruction (str "1. Get the user request classified.\n"
"2. If the classification is \"technical-support\", call technical help. "
"Otherwise, call price lookup.")
:Input :Customer.Support.Core/CameraStore}}
"Otherwise, call price lookup.")}}

;; Usage:
;; POST api/Customer.Support.Core/CameraStore
;; {"Customer.Support.Core/CameraStore": {"UserInstruction": "What's the price of Panasonic G9?"}}
;; POST api/Customer.Support.Core/CameraSupportAgent
;; {"Customer.Support.Core/CameraSupportAgent": {"UserInstruction": "What's the price of Panasonic G9?"}}
20 changes: 9 additions & 11 deletions example/agents/expense/expense/workflow.al
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,30 @@

(event
:ReceiptImageToExpenseReport
{:meta {:doc "Returns a textual expense report"}
{:meta {:doc "Returns a textual expense report, and not an instance with attributes like UserInstruction."}
:Url :String})

{:Agentlang.Core/Agent
{:Name :receipt-ocr-agent
:Type :ocr
{:Name :Expense.Workflow/ReceiptImageToExpenseReportAgent
:Features [:ocr]
:UserInstruction (str "Analyse the image of a receipt and return only the items and their amounts. "
"No need to include sub-totals, totals and other data.")
:Input :Expense.Workflow/ReceiptImageToExpenseReport
:LLM :llm01}}

{:Agentlang.Core/Agent
{:Name :convert-report-to-expense-agent
:Type :planner
{:Name :Expense.Workflow/ConvertReportToExpenseAgent
:UserInstruction "Convert an expense report to individual instances of the expense entity."
:Tools [:Expense.Workflow/Expense]}}

{:Agentlang.Core/Agent
{:Name :expense-agent
:Type :planner
{:Name :Expense.Workflow/SaveExpensesAgent
:LLM :llm01
:UserInstruction (str "1. Extract an expense report from the given receipt image url.\n"
"2. Convert this report to individual expenses.")
:Input :Expense.Workflow/SaveExpenses
:Delegates [:receipt-ocr-agent :convert-report-to-expense-agent]}}
:Delegates [:Expense.Workflow/ReceiptImageToExpenseReportAgent
:Expense.Workflow/ConvertReportToExpenseAgent]}}

;; Usage:
;; POST api/Expense.Workflow/SaveExpenses
;; {"Expense.Workflow/SaveExpenses": {"UserInstruction": "https://acme.com/bill/myexpense.jpg"}}
;; POST api/Expense.Workflow/SaveExpensesAgent
;; {"Expense.Workflow/SaveExpensesAgent": {"UserInstruction": "https://acme.com/bill/myexpense.jpg"}}
15 changes: 7 additions & 8 deletions example/agents/recruitment/recruitment/workflow.al
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
:UserInstruction :String})

{:Agentlang.Core/Agent
{:Name :profile-summary-agent
{:Name :Recruitment.Workflow/CreateProfileSummaryTextFromResumeAgent
:Input :Recruitment.Workflow/CreateProfileSummaryTextFromResume
:UserInstruction (str "You are a recruiter that analyses a resume and provides a summary of the "
"skills and experience of the candidate. The summary must be in the format - "
Expand All @@ -84,28 +84,27 @@
:UserInstruction :String})

{:Agentlang.Core/Agent
{:Name :check-summary-agent
{:Name :Recruitment.Workflow/CheckProfileSummaryAgent
:LLM :llm01
:Input :Recruitment.Workflow/CheckProfileSummary
:UserInstruction
(str "If the profile passed to you is for an experienced C++ programmer, return `yes`, otherwise return `no`.")}}

{:Agentlang.Core/Agent
{:Name :interview-director-agent
:Type :planner
{:Name :Recruitment.Workflow/InterviewDirectorAgent
:LLM :llm01
:Input :Recruitment.Workflow/InvokeAgent
:UserInstruction (str "1. Create a profile-summary text from the given resume.\n"
"2. Check the profile-summary text from step (1) by calling the CheckProfileSummary event.\n"
"3. If the result of step (2) is \"yes\", then schedule an interview for the candidate. Otherwise, reject the candidate's profile. "
"(An interview may be assigned to one of sam@acme.com, joe@amce.com and susan@acme.com).\n"
"Do not skip any of the steps 1, 2 and 3.\n")
:Tools [:Recruitment.Workflow/ScheduleInterview :Recruitment.Workflow/RejectProfile]
:Delegates [:profile-summary-agent :check-summary-agent]}}
:Delegates [:Recruitment.Workflow/CreateProfileSummaryTextFromResumeAgent
:Recruitment.Workflow/CheckProfileSummaryAgent]}}

;; Usage:
;; POST api/Recruitment.Workflow/InvokeAgent
;; {"Recruitment.Workflow/InvokeAgent":
;; POST api/Recruitment.Workflow/InterviewDirectorAgent
;; {"Recruitment.Workflow/InterviewDirectorAgent":
;; {"UserInstruction": "Here's a resume: <some-resume-text>"}}

(def slot-data
Expand Down
10 changes: 4 additions & 6 deletions example/agents/weather_service/weather/service/core.al
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,14 @@
:CompletionModel "gpt-3.5-turbo"}}}

{:Agentlang.Core/Agent
{:Name :weather-planner-agent
:Type :planner
{:Name :Weather.Service.Core/WeatherPlannerAgent
:Tools [:Weather.Service.Core/GetWeatherForCity]
:UserInstruction "You are an agent that figures out which tool to use to answer a user query."
:LLM :llm01
:Input :Weather.Service.Core/InvokePlanner}}
:LLM :llm01}}

;; Usage:
;; POST api/Weather.Service.Core/InvokePlanner
;; {"Weather.Service.Core/InvokePlanner": {"UserInstruction": "What's the weather for Boston today?"}}
;; POST api/Weather.Service.Core/WeatherPlannerAgent
;; {"Weather.Service.Core/WeatherPlannerAgent": {"UserInstruction": "What's the weather for Boston today?"}}

(dataflow
:Agentlang.Kernel.Lang/AppInit
Expand Down
5 changes: 4 additions & 1 deletion src/agentlang/inference/service/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
(us/nonils
(mapv (fn [[agent-name spec]]
(when-let [docs (:Documents spec)]
[(name agent-name) (mapv preproc-doc-spec docs)]))
[(if (string? agent-name)
agent-name
(subs (str agent-name) 1))
(mapv preproc-doc-spec docs)]))
agents))))

(defn setup-agent-documents []
Expand Down
61 changes: 46 additions & 15 deletions src/agentlang/inference/service/model.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,19 @@
(def ^:private feature-set {"chain-of-thought" ft-chain-of-thought
"self-critique" ft-self-critique})

(def ^:private feature-set-keys (set (keys feature-set)))
(def ^:private feature-set-keys (set (concat (keys feature-set) ["planner" "ocr"])))

(defn- feature-list? [xs]
(when (seq xs)
(and (vector? xs)
(= feature-set-keys (set/union feature-set-keys (set xs))))))

(defn- has-feature? [x xs]
(some #{x} (map u/keyword-as-string xs)))

(def ^:private has-planner? (partial has-feature? "planner"))
(def ^:private has-ocr? (partial has-feature? "ocr"))

(defn get-feature-prompt [ft] (get feature-set ft ""))

(record
Expand Down Expand Up @@ -174,10 +180,8 @@
(= typ (:Type agent-instance)))

(def ocr-agent? (partial agent-of-type? "ocr"))
(def classifier-agent? (partial agent-of-type? "classifier"))
(def planner-agent? (partial agent-of-type? "planner"))
(def agent-gen-agent? (partial agent-of-type? "agent-gen"))
(def eval-agent? (partial agent-of-type? "eval"))

(defn- eval-event
([event callback atomic?]
Expand All @@ -204,8 +208,11 @@
tools)))

(defn- as-event-name [agent-name]
(let [n (csk/->PascalCase agent-name)]
(cn/canonical-type-name n)))
(if (and (keyword? agent-name)
(= 2 (count (li/split-path agent-name))))
agent-name
(let [n (csk/->PascalCase agent-name)]
(cn/canonical-type-name n))))

(defn- preproc-agent-input-spec [agent-name input]
(if input
Expand Down Expand Up @@ -235,11 +242,6 @@
(assoc agent-instance :UserInstruction s)
agent-instance))

(defn- verify-name [n]
(when (or (s/index-of n "/") (s/index-of n "."))
(u/throw-ex (str "Invalid name " n ", cannot contain `/` or `.`")))
n)

(defn- fetch-channel-tools [channel]
(when-let [tools (get-in (cn/fetch-model channel) [:channel :tools])]
(preproc-agent-tools-spec tools)))
Expand All @@ -253,14 +255,35 @@
(when (seq delegs)
(mapv u/keyword-as-string delegs)))

(defn- maybe-cast-to-planner [attrs]
(cond
(= :planner (u/string-as-keyword (:Type attrs)))
attrs

(or (seq (:Tools attrs))
(seq (:Delegates attrs)))
(assoc attrs :Type :planner)

(seq (:Features attrs))
(let [fs (:Features attrs)]
(cond
(has-planner? fs)
(assoc attrs :Type :planner)
(has-ocr? fs)
(assoc attrs :Type :ocr)
:else attrs))

:else attrs))

(ln/install-standalone-pattern-preprocessor!
:Agentlang.Core/Agent
(fn [pat]
(let [attrs (li/record-attributes pat)
(let [attrs (maybe-cast-to-planner (li/record-attributes pat))
nm (:Name attrs)
input (preproc-agent-input-spec nm (:Input attrs))
tools (preproc-agent-tools-spec (:Tools attrs))
delegates (preproc-agent-delegates (:Delegates attrs))
features (when-let [ftrs (:Features attrs)] (mapv u/keyword-as-string ftrs))
tp (:Type attrs)
llm (or (:LLM attrs) {:Type "openai"})
docs (:Documents attrs)
Expand All @@ -269,21 +292,29 @@
new-attrs
(-> attrs
(cond->
nm (assoc :Name (verify-name (u/keyword-as-string nm)))
nm (assoc :Name (u/keyword-as-string nm))
input (assoc :Input input)
tools (assoc :Tools tools)
delegates (assoc :Delegates delegates)
docs (assoc :Documents (preproc-agent-docs docs))
tp (assoc :Type (u/keyword-as-string tp))
features (assoc :Features features)
channels (assoc :Channels (mapv name channels))
llm (assoc :LLM (u/keyword-as-string llm))))]
(when (seq channels)
(maybe-register-subscription-handlers! channels (keyword input)))
(assoc pat :Agentlang.Core/Agent
(cond
(planner-agent? new-attrs) (planner/with-instructions new-attrs)
(agent-gen-agent? new-attrs) (agent-gen/with-instructions new-attrs)
(classifier-agent? new-attrs) (classifier-with-instructions new-attrs)
(planner-agent? new-attrs)
#?(:clj
(planner/with-instructions new-attrs)
:cljs
(log/error (str "Shouldn't be executed for cljs runtime with attrs: " new-attrs)))
(agent-gen-agent? new-attrs)
#?(:clj
(agent-gen/with-instructions new-attrs)
:cljs
(log/error (str "Shouldn't be executed for cljs runtime with attrs: " new-attrs)))
:else new-attrs)))))

(defn maybe-define-inference-event [event-name]
Expand Down
4 changes: 2 additions & 2 deletions src/agentlang/inference/service/planner.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@
(defn- parse-update [[n attrs new-attrs] alias]
(let [qexpr (parse-lookup-one [n attrs] nil)
qattrs (li/record-attributes qexpr)]
{n (merge qattrs (parse-value-refs-and-exprs new-attrs)
(when alias {:as alias}))}))
(merge {n (merge qattrs (parse-value-refs-and-exprs new-attrs))}
(when alias {:as alias}))))

(defn- parse-delete [[n attrs] alias]
(let [pat [:delete n attrs]]
Expand Down
Loading