Skip to content

Instantly share code, notes, and snippets.

@Dhravya
Created July 1, 2024 18:26
Show Gist options
  • Save Dhravya/802e1efca61a6cf8ae5037b348cf94fd to your computer and use it in GitHub Desktop.
Save Dhravya/802e1efca61a6cf8ae5037b348cf94fd to your computer and use it in GitHub Desktop.
import { createToolsFromOpenAPISpec, runWithTools } from "@cloudflare/ai-utils";
export default {
async fetch(request, env, ctx) {
const response = (await runWithTools(
env.AI,
"@hf/nousresearch/hermes-2-pro-mistral-7b",
{
messages: [
{
role: "user",
content: "Who is Cloudflare on github?",
},
],
tools: [
// You can pass the OpenAPI spec link or contents directly
...(await createToolsFromOpenAPISpec(
"https://gist.githubusercontent.com/mchenco/fd8f20c8f06d50af40b94b0671273dc1/raw/f9d4b5cd5944cc32d6b34cad0406d96fd3acaca6/partial_api.github.com.json",
{
overrides: [
{
matcher: ({ url, method }) => {
return url.hostname === "api.github.com";
},
// for all requests on *.github.com, we'll need to add a User-Agent.
values: {
headers: {
"User-Agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36",
},
},
},
],
},
)),
],
},
{
streamFinalResponse: true,
},
)) as ReadableStream;
return new Response(response, {
headers: {
"content-type": "text/event-stream",
},
});
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment