Skip to content

Commit

Permalink
support rendering tools in notepad
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Jun 15, 2024
1 parent f912bc2 commit 3a70f7a
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 12 deletions.
125 changes: 116 additions & 9 deletions docs/src/content/docs/guides/tool-agent.mdx
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
---
title: Tool Agent
sidebar:
order: 7
description: Learn how to define a built-in agent using functions for decision-making and reasoning in arithmetic operations.
keywords: tool agent, arithmetic agent, function declaration, script parameters, system.math
order: 7
description: Learn how to define a built-in agent using functions for
decision-making and reasoning in arithmetic operations.
keywords: tool agent, arithmetic agent, function declaration, script parameters,
system.math
genaiscript:
model: openai:gpt-3.5-turbo

---

import { Code } from "@astrojs/starlight/components"
import mathAgentSrc from "../../../../../packages/sample/genaisrc/math-agent.genai.js?raw"
import mathAgentSystemSrc from "../../../../../packages/sample/genaisrc/math-agent-system.genai.js?raw"

Using [functions (tools)](/genaiscript/reference/functions),
Using [tools (formerly functions))](/genaiscript/reference/tools),
you can define a built-in agent that can take decisions
and reasoning based on the tools provided to it.

Expand All @@ -27,7 +32,7 @@ is required.
```js "defTool"
defTool(
"sum",
"Use this function to sum two numbers",
"Sum two numbers",
{
type: "object",
properties: {
Expand Down Expand Up @@ -72,17 +77,119 @@ $`Answer the following arithmetic question:
`
```

Putting it all together, we define another function to divide two numbers
Putting it all together, we define another tool to divide two numbers
and inline an arithmetic question.

<Code title="math-agent.genai.js" code={mathAgentSrc} wrap={true} lang="js" />
```js wrap
script({
title: "math-agent",
model: "openai:gpt-35-turbo",
description: "A port of https://ts.llamaindex.ai/examples/agent",
parameters: {
"question": {
type: "string",
default: "How much is 11 + 4? then divide by 3?"
},
},
tests: {
description: "Testing the default prompt",
keywords: "5"
}
})

defTool("sum", "Use this function to sum two numbers", {
type: "object",
properties: {
a: {
type: "number",
description: "The first number",
},
b: {
type: "number",
description: "The second number",
},
},
required: ["a", "b"],
}, ({ a, b }) => `${a + b}`)

defTool("divide", "Use this function to divide two numbers", {
type: "object",
properties: {
a: {
type: "number",
description: "The first number",
},
b: {
type: "number",
description: "The second number",
},
},
required: ["a", "b"],
}, ({ a, b }) => `${a / b}`)

$`Answer the following arithmetic question:
${env.vars.question}
`
```

{/* genaiscript output start */}

<details>
<summary>👤 user</summary>


```markdown wrap
Answer the following arithmetic question:

How much is 11 + 4? then divide by 3?
```


</details>


<details open>
<summary>🤖 assistant </summary>

- 📠 tool call `divide({"a":15,"b":3})` (`call_9p0oWdWpT6vGyxzwq2vJXHrq`)

</details>


<details>
<summary>🛠️ tool <code>call_9p0oWdWpT6vGyxzwq2vJXHrq</code></summary>


```json wrap
5
```


</details>


<details open>
<summary>🤖 assistant </summary>


```markdown wrap
The result of (11 + 4) divided by 3 is 5.
```


</details>

{/* genaiscript output end */}



## Using `system.math`

The system prompt [system.math](/genaiscript/reference/scripts/system#systemmath)
wraps the `parsers.math` expression parser and evaluator and exposes it as a function.
wraps the `parsers.math` expression parser and evaluator and exposes it as a tool.

This simplifies the agent script as we do not have to define functions anymore.
This simplifies the agent script as we do not have to define tools anymore.

<Code
title="math-agent.genai.js"
Expand Down
16 changes: 13 additions & 3 deletions packages/core/src/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -649,8 +649,18 @@ export function renderMessagesToMarkdown(
case "assistant":
res.push(
details(
`🤖 assistant`,
fenceMD(msg.content, "markdown"),
`🤖 assistant ${msg.name ? msg.name : ""}`,
[
fenceMD(msg.content, "markdown"),
msg.tool_calls
?.map(
(tc) =>
`- 📠 tool call \`${tc.function.name}(${tc.function.arguments})\` (\`${tc.id}\`)`
)
.join("\n"),
]
.filter((s) => !!s)
.join("\n\n"),
assistant === true
)
)
Expand All @@ -661,7 +671,7 @@ export function renderMessagesToMarkdown(
case "tool":
res.push(
details(
`🛠️ tool ${msg.tool_call_id}`,
`🛠️ tool <code>${msg.tool_call_id}</code>`,
fenceMD(msg.content, "json")
)
)
Expand Down

0 comments on commit 3a70f7a

Please sign in to comment.