Skip to content

Commit

Permalink
feat: http request tool + example checking on the latest fabrice is…
Browse files Browse the repository at this point in the history
…sues (#139)

Related to #74 
TODO: add `bdd` tests after merging #137 in
  • Loading branch information
pkarw authored Dec 20, 2024
1 parent e871953 commit 66bc887
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
46 changes: 46 additions & 0 deletions example/src/fabrice_latest_issues.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import 'dotenv/config'

import { httpTool } from '@fabrice-ai/tools/http'
import { agent } from 'fabrice-ai/agent'
import { solution } from 'fabrice-ai/solution'
import { teamwork } from 'fabrice-ai/teamwork'
import { logger } from 'fabrice-ai/telemetry'
import { workflow } from 'fabrice-ai/workflow'

const browser = agent({
description: `
You are skilled at browsing Web with specified URLs,
methods, params etc.
You are using "httpTool" to get the data from the API and/or Web pages.
`,
tools: {
httpTool,
},
})

const wrapupRedactor = agent({
description: `
Your role is to check Github project details and check for latest issues.
`,
})

const checkupGithubProject = workflow({
team: { browser, wrapupRedactor },
description: `
Check the project details for "fabrice-ai" using the following API URL:
"https://api.github.com/repos/callstackincubator/fabrice-ai".
From the data received get the number of stars and the URL for the listing the issues.
List last top 3 issues and the number of star gazers for the project.
`,
output: `
Comprehensive markdown report for fabrice-ai project:
- Include top 3 new issues.
- Include the actual number of star gazers.
`,
snapshot: logger,
})

const result = await teamwork(checkupGithubProject)

console.log(solution(result))
53 changes: 53 additions & 0 deletions packages/tools/src/http.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import axios from 'axios'
import { tool } from 'fabrice-ai/tool'
import { z } from 'zod'

async function makeHttpRequest({
url,
method,
headers,
body,
}: {
url: string
method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'
headers?: Record<string, string>
body?: any
}): Promise<any> {
try {
const response = await axios({
url,
method,
headers,
data: body,
})

if (typeof response.data === 'object') {
return JSON.stringify(response.data)
} else return response.data as string
} catch (error) {
if (axios.isAxiosError(error)) {
throw new Error(
`HTTP error ${error.response?.status}: ${error.response?.data || error.message}`
)
} else {
throw new Error(`Unknown error: ${error}`)
}
}
}

export const httpTool = tool({
description: 'Makes HTTP requests to specified URLs with configurable method, headers, and body.',
parameters: z.object({
url: z.string().describe('The URL to make the request to.'),
method: z.enum(['GET', 'POST', 'PUT', 'DELETE', 'PATCH']).describe('The HTTP method to use.'),
headers: z.record(z.string()).optional().describe('Headers to include in the HTTP request.'),
body: z
.string()
.describe(
'The body of the HTTP request. For GET requests, this should typically be empty string. For other requests it could be JSON or other formats.'
),
}),
execute: async ({ url, method, headers, body }) => {
return makeHttpRequest({ url, method, headers, body })
},
})

0 comments on commit 66bc887

Please sign in to comment.