-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: http request tool + example checking on the latest
fabrice
is…
- Loading branch information
Showing
2 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
}, | ||
}) |