Skip to content

Commit

Permalink
🐛 remove axios from dep to adpter edge on nextjs, close #5
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannLai committed Jun 27, 2023
1 parent 7f44192 commit efc5e3a
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 59 deletions.
1 change: 0 additions & 1 deletion examples/webbrowser.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const { Configuration, OpenAIApi } = require("openai");
const {
createWebBrowser,
createGoogleCustomSearch,
createClock,
} = require("../dist/cjs/index.js");

Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@
"license": "MIT",
"repository": "johannlai/openai-function-calling-tools",
"dependencies": {
"axios": "^1.4.0",
"cheerio": "1.0.0-rc.12",
"esprima": "^4.0.1",
"expr-eval": "^2.0.2",
Expand Down
22 changes: 2 additions & 20 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion tools/aiplugin.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { createAIPlugin } from './aiplugin';
import axios from 'axios';
import { expect, it } from 'vitest';

it('should create a new AIPlugin', async () => {
Expand Down
17 changes: 10 additions & 7 deletions tools/aiplugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import axios from 'axios';
import { Tool } from './tool';
import { z } from 'zod';

Expand All @@ -24,13 +23,17 @@ async function createAIPlugin({
product: z.string().optional(),
});

const aiPluginRes = await axios.get(url).then(res => {
return res.data as AIPluginRes
})
const aiPluginResRes = await fetch(url);
if (!aiPluginResRes.ok) {
throw new Error(`HTTP error! status: ${aiPluginResRes.status}`);
}
const aiPluginRes = await aiPluginResRes.json() as AIPluginRes;

const apiUrlRes = await axios.get(aiPluginRes.api.url).then(res => {
return res.data as any
})
const apiUrlResRes = await fetch(aiPluginRes.api.url);
if (!apiUrlResRes.ok) {
throw new Error(`HTTP error! status: ${apiUrlResRes.status}`);
}
const apiUrlRes = await apiUrlResRes.json() as any;

const execute = async () => {
return `
Expand Down
15 changes: 10 additions & 5 deletions tools/googleCustomSearch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import axios from 'axios';
import { Tool } from './tool';
import { z } from 'zod';

Expand All @@ -15,25 +14,31 @@ function createGoogleCustomSearch({ apiKey, googleCSEId }: { apiKey: string; goo

const execute = async ({ input }: z.infer<typeof paramsSchema>) => {
try {
const res = await axios.get(
const res = await fetch(
`https://www.googleapis.com/customsearch/v1?key=${apiKey}&cx=${googleCSEId}&q=${encodeURIComponent(
input
)}`
);

if (!res.ok) {
throw new Error(`HTTP error! status: ${res.status}`);
}

const data = await res.json();

const results =
res.data?.items?.map((item: { title?: string; link?: string; snippet?: string }) => ({
data.items?.map((item: { title?: string; link?: string; snippet?: string }) => ({
title: item.title,
link: item.link,
snippet: item.snippet,
})) ?? [];
return JSON.stringify(results);
} catch(error) {
} catch (error) {
throw new Error(`Error in GoogleCustomSearch: ${error}`);
}
};

return new Tool(paramsSchema, name, description, execute).tool;
}

export { createGoogleCustomSearch };
export { createGoogleCustomSearch };
22 changes: 12 additions & 10 deletions tools/request.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
import axios from 'axios';
import { Tool } from './tool';
import { z } from 'zod';

function createRequest(baseOption: {
url?: string;
method?: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
data?: Record<string, any>;
body?: Record<string, any>;
headers?: Record<string, string>;
} = {}) {
const paramsSchema = z.object({
url: z.string(),
method: z.enum(['GET', 'POST', 'PUT', 'PATCH', 'DELETE']),
data: z.record(z.any()).optional(),
body: z.record(z.any()).optional(),
headers: z.record(z.string()).optional(),
});
const name = 'request';
const description = `Useful for sending http request.
Use this when you need to get specific content from a url.
Input is a url, method, data, headers, output is the result of the request.
Input is a url, method, body, headers, output is the result of the request.
`;

const execute = async ({ url, method, data, headers }: z.infer<typeof paramsSchema>) => {
const execute = async ({ url, method, body, headers }: z.infer<typeof paramsSchema>) => {
try {
const res = await axios({
url: url || baseOption.url,
method: method || baseOption.method,
data: data || baseOption.data,
const res = await fetch(url || baseOption.url!, {
method: method || baseOption.method!,
body: JSON.stringify(body || baseOption.body),
headers: headers || baseOption.headers,
});

return res.data;
if (!res.ok) {
throw new Error(`HTTP error! status: ${res.status}`);
}

return await res.json();
} catch (error) {
return `Failed to execute script: ${error.message}`;
}
Expand Down
28 changes: 14 additions & 14 deletions tools/webbrowser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import axiosMod, { AxiosRequestConfig, AxiosStatic } from "axios";
import { Tool } from './tool';
import { z } from 'zod';
import * as cheerio from "cheerio";
Expand All @@ -25,27 +24,28 @@ function createWebBrowser() {
const name = 'webbrowser';
const description = 'useful for when you need to summarize a webpage. input should be a ONE valid http URL including protocol.';

const execute = async ({ url }: z.infer<typeof paramsSchema>) => {
const axios = ("default" in axiosMod ? axiosMod.default : axiosMod) as AxiosStatic;
const config: AxiosRequestConfig = {
headers: DEFAULT_HEADERS,
};

const execute = async ({ url }: z.infer<typeof paramsSchema>) => {
try {
const htmlResponse = await axios.get(url);
const htmlResponse = await fetch(url, { headers: DEFAULT_HEADERS });

if (!htmlResponse.ok) {
throw new Error(`HTTP error! status: ${htmlResponse.status}`);
}

const contentType = htmlResponse.headers.get("content-type");
const allowedContentTypes = ["text/html", "application/json", "application/xml", "application/javascript", "text/plain"];
const contentType = htmlResponse.headers["content-type"];
const contentTypeArray = contentType.split(";");
if (contentTypeArray[0] && !allowedContentTypes.includes(contentTypeArray[0])) {
const contentTypeArray = contentType?.split(";");

if (contentTypeArray && contentTypeArray[0] && !allowedContentTypes.includes(contentTypeArray[0])) {
throw new Error("returned page was not utf8");
}

const html = htmlResponse.data;
const html = await htmlResponse.text();
const $ = cheerio.load(html, { scriptingEnabled: true });
let text = "";
const rootElement = "body";

$(`${rootElement}:not(style):not(script):not(svg)`).each((_i:any, elem: any) => {
$(`${rootElement}:not(style):not(script):not(svg)`).each((_i: any, elem: any) => {
let content = $(elem).text().trim();
const $el = $(elem);
let href = $el.attr("href");
Expand All @@ -69,7 +69,7 @@ function createWebBrowser() {

return text.trim().replace(/\n+/g, " ");
} catch (error) {
throw new Error(`Error in getHtml: ${error}`);
return `Error in get content of web: ${error.message}`;
}
};

Expand Down

0 comments on commit efc5e3a

Please sign in to comment.