few changes

This commit is contained in:
zaidmukaddam 2024-08-16 23:21:42 +05:30
parent 56513a8ced
commit ddbe2f44de
2 changed files with 115 additions and 85 deletions

View File

@ -1,8 +1,8 @@
import { anthropic } from '@ai-sdk/anthropic' import { anthropic } from "@ai-sdk/anthropic";
import { convertToCoreMessages, streamText, tool } from "ai"; import { convertToCoreMessages, streamText, tool } from "ai";
import { CodeInterpreter } from '@e2b/code-interpreter' import { CodeInterpreter } from "@e2b/code-interpreter";
import { z } from "zod"; import { z } from "zod";
import { geolocation } from '@vercel/functions' import { geolocation } from "@vercel/functions";
// Allow streaming responses up to 30 seconds // Allow streaming responses up to 30 seconds
export const maxDuration = 60; export const maxDuration = 60;
@ -21,6 +21,7 @@ export async function POST(req: Request) {
system: system:
"You are an AI web search engine that helps users find information on the internet.\n" + "You are an AI web search engine that helps users find information on the internet.\n" +
"Always start with running the tool(s) and then and then only write your response AT ALL COSTS!!\n" + "Always start with running the tool(s) and then and then only write your response AT ALL COSTS!!\n" +
"Your basic task to first and foremost gather information from a tool and then write your response with images(with web_search)!.\n" +
"Never write a response without running the tool(s) first!\n" + "Never write a response without running the tool(s) first!\n" +
"Do not announce or inform the user in any way that your going to run a tool at ALL COSTS!! Just `run` it and then write your response AT ALL COSTS!!!!!." + "Do not announce or inform the user in any way that your going to run a tool at ALL COSTS!! Just `run` it and then write your response AT ALL COSTS!!!!!." +
"Tool Usage Instructions:\n" + "Tool Usage Instructions:\n" +
@ -30,14 +31,14 @@ export async function POST(req: Request) {
"Incomplete details or any other said words before the search tool result in a bad response." + "Incomplete details or any other said words before the search tool result in a bad response." +
"Always call the 'web_search' tool to get the information, no need to do a chain of thought or say anything else, go straight to the point." + "Always call the 'web_search' tool to get the information, no need to do a chain of thought or say anything else, go straight to the point." +
"Once you have found the information, you provide the user with the information you found in brief like a news paper detail." + "Once you have found the information, you provide the user with the information you found in brief like a news paper detail." +
"The detail should be 3-5 paragraphs in 10-12 sentences and put citations using the markdown link format like this always: [source text](link to the site) in the end of each paragraph!" + "The detail should be 3-5 paragraphs in 10-12 sentences and put citations using the markdown link format like this always: [<source text>](link to the site) in the end of each paragraph!" +
"Citations will the render in the client side, so please make sure you format them correctly!" + "Citations will the render in the client side, so please make sure you format them correctly!" +
"Never use pointers, unless asked to." + "Never use pointers, unless asked to." +
"Do not start the responses with newline characters, always start with the first sentence." + "Do not start the responses with newline characters, always start with the first sentence." +
"When the user asks about a Stock, you should 'always' first gather news about it with web search tool, then show the chart and then write your response. Follow these steps in this order only!" + "When the user asks about a Stock, you should 'always' first gather news about it with web search tool, then show the chart and then write your response. Follow these steps in this order only!" +
"Never use the retrieve tool for general search. Always use it when the user provides an url! " + "Never use the retrieve tool for general search. Always use it when the user provides an url! " +
"For weather related questions, use get_weather_data tool and write your response. No need to call any other tool. DO NOT put citation to OpenWeatherMaps API EVER!" + "For weather related questions, use get_weather_data tool and write your response. No need to call any other tool. DO NOT put citation to OpenWeatherMaps API EVER!" +
"Use the 'programming' tool to execute Python code for cases like calculating, sorting, etc. that require computation. " + "Use the 'programming' tool to execute Python code for cases like calculating, sorting, etc. that require computation. " +
"The environment is like a jupyter notebook so don't write print statements at all costs!, just write variables in the end.\n\n" + "The environment is like a jupyter notebook so don't write print statements at all costs!, just write variables in the end.\n\n" +
"The current date is: " + "The current date is: " +
new Date() new Date()
@ -50,8 +51,9 @@ export async function POST(req: Request) {
.replace(/(\w+), (\w+) (\d+), (\d+)/, "$4-$2-$3 ($1)") + .replace(/(\w+), (\w+) (\d+), (\d+)/, "$4-$2-$3 ($1)") +
"." + "." +
"Rules for the response:\n" + "Rules for the response:\n" +
"Never use tags like <response>, <result> or <answer> in the response." + "Please do not use tags like <response>, <result> or <answer> in the response. it breaks the reader's attention to these stupid mistakes." +
"Use a story telling format in your response, like a news article ALWAYS! This is for all tools except programming!" + "Remember to always use the 'web_search' tool to gather information before writing a response." +
"You are a graduate level AI, so please write responses accordingly." +
"Never start with 'based on the search results,...' EVER! Always start with the information you found like an article!" + "Never start with 'based on the search results,...' EVER! Always start with the information you found like an article!" +
"Never use the heading format in your response!." + "Never use the heading format in your response!." +
"Do not use print statements in the code execution tool, just the variables." + "Do not use print statements in the code execution tool, just the variables." +
@ -59,158 +61,186 @@ export async function POST(req: Request) {
"IMPORTANT!!!: Refrain from saying things like that mention that your going to perform a certain action, example: 'Certainly! I'll search for information about <something> using the web search tool.'", "IMPORTANT!!!: Refrain from saying things like that mention that your going to perform a certain action, example: 'Certainly! I'll search for information about <something> using the web search tool.'",
tools: { tools: {
web_search: tool({ web_search: tool({
description: 'Search the web for information with the given query, max results and search depth.', description:
"Search the web for information with the given query, max results and search depth.",
parameters: z.object({ parameters: z.object({
query: z.string() query: z.string().describe("The search query to look up on the web."),
.describe('The search query to look up on the web.'), maxResults: z
maxResults: z.number() .number()
.describe('The maximum number of results to return. Default to be used is 10.'), .describe(
searchDepth: "The maximum number of results to return. Default to be used is 10.",
z.enum(['basic', 'advanced']) ),
.describe('The search depth to use for the search. Default is basic.') searchDepth: z
.enum(["basic", "advanced"])
.describe(
"The search depth to use for the search. Default is basic.",
),
exclude_domains: z
.array(z.string())
.optional()
.describe(
"A list of domains to specifically exclude from the search results. Default is None, which doesn't exclude any domains.",
),
}), }),
execute: async ({ query, maxResults, searchDepth }: { query: string, maxResults: number, searchDepth: 'basic' | 'advanced' }) => { execute: async ({
const apiKey = process.env.TAVILY_API_KEY query,
const response = await fetch('https://api.tavily.com/search', { maxResults,
method: 'POST', searchDepth,
exclude_domains,
}: {
query: string;
maxResults: number;
searchDepth: "basic" | "advanced";
exclude_domains?: string[];
}) => {
const apiKey = process.env.TAVILY_API_KEY;
const response = await fetch("https://api.tavily.com/search", {
method: "POST",
headers: { headers: {
'Content-Type': 'application/json' "Content-Type": "application/json",
}, },
body: JSON.stringify({ body: JSON.stringify({
api_key: apiKey, api_key: apiKey,
query, query,
max_results: maxResults < 5 ? 5 : maxResults, max_results: maxResults < 5 ? 5 : maxResults,
search_depth: searchDepth, search_depth: searchDepth,
include_images: true, include_answers: true,
include_answers: true exclude_domains: exclude_domains,
}) }),
}) });
const data = await response.json() const data = await response.json();
let context = data.results.map((obj: { url: any; content: any; title: any; raw_content: any; }) => { let context = data.results.map(
return { (obj: { url: any; content: any; title: any; raw_content: any }) => {
url: obj.url, return {
title: obj.title, url: obj.url,
content: obj.content, title: obj.title,
raw_content: obj.raw_content content: obj.content,
} raw_content: obj.raw_content,
}) };
},
);
return { return {
results: context results: context,
} };
} },
}), }),
retrieve: tool({ retrieve: tool({
description: 'Retrieve the information from a URL.', description: "Retrieve the information from a URL.",
parameters: z.object({ parameters: z.object({
url: z.string().describe('The URL to retrieve the information from.') url: z.string().describe("The URL to retrieve the information from."),
}), }),
execute: async ({ url }: { url: string }) => { execute: async ({ url }: { url: string }) => {
let hasError = false let hasError = false;
let results; let results;
try { try {
const response = await fetch(`https://r.jina.ai/${url}`, { const response = await fetch(`https://r.jina.ai/${url}`, {
method: 'GET', method: "GET",
headers: { headers: {
Accept: 'application/json', Accept: "application/json",
'X-With-Generated-Alt': 'true' "X-With-Generated-Alt": "true",
} },
}) });
const json = await response.json() const json = await response.json();
if (!json.data || json.data.length === 0) { if (!json.data || json.data.length === 0) {
hasError = true hasError = true;
} else { } else {
// Limit the content to 5000 characters // Limit the content to 5000 characters
if (json.data.content.length > 5000) { if (json.data.content.length > 5000) {
json.data.content = json.data.content.slice(0, 5000) json.data.content = json.data.content.slice(0, 5000);
} }
results = { results = {
results: [ results: [
{ {
title: json.data.title, title: json.data.title,
content: json.data.content, content: json.data.content,
url: json.data.url url: json.data.url,
} },
], ],
query: '', query: "",
images: [] images: [],
} };
} }
} catch (error) { } catch (error) {
hasError = true hasError = true;
console.error('Retrieve API error:', error) console.error("Retrieve API error:", error);
} }
if (hasError || !results) { if (hasError || !results) {
return results return results;
} }
return results return results;
} },
}), }),
get_weather_data: tool({ get_weather_data: tool({
description: "Get the weather data for the given coordinates.", description: "Get the weather data for the given coordinates.",
parameters: z.object({ parameters: z.object({
lat: z.number().describe('The latitude of the location.'), lat: z.number().describe("The latitude of the location."),
lon: z.number().describe('The longitude of the location.') lon: z.number().describe("The longitude of the location."),
}), }),
execute: async ({ lat, lon }: { lat: number, lon: number }) => { execute: async ({ lat, lon }: { lat: number; lon: number }) => {
const apiKey = process.env.OPENWEATHER_API_KEY const apiKey = process.env.OPENWEATHER_API_KEY;
const response = await fetch(`https://api.openweathermap.org/data/2.5/forecast?lat=${lat}&lon=${lon}&appid=${apiKey}`) const response = await fetch(
const data = await response.json() `https://api.openweathermap.org/data/2.5/forecast?lat=${lat}&lon=${lon}&appid=${apiKey}`,
return data );
} const data = await response.json();
return data;
},
}), }),
stock_chart_ui: tool({ stock_chart_ui: tool({
description: 'Display the stock chart for the given stock symbol after web search.', description:
"Display the stock chart for the given stock symbol after web search.",
parameters: z.object({ parameters: z.object({
symbol: z.string().describe('The stock symbol to display the chart for.') symbol: z
.string()
.describe("The stock symbol to display the chart for."),
}), }),
}), }),
programming: tool({ programming: tool({
description: 'Write and execute Python code.', description: "Write and execute Python code.",
parameters: z.object({ parameters: z.object({
code: z.string().describe('The Python code to execute.') code: z.string().describe("The Python code to execute."),
}), }),
execute: async ({ code }: { code: string }) => { execute: async ({ code }: { code: string }) => {
const sandbox = await CodeInterpreter.create() const sandbox = await CodeInterpreter.create();
const execution = await sandbox.notebook.execCell(code) const execution = await sandbox.notebook.execCell(code);
if (execution.results.length > 0) { if (execution.results.length > 0) {
let message: string = ''; let message: string = "";
for (const result of execution.results) { for (const result of execution.results) {
if (result.isMainResult) { if (result.isMainResult) {
message += `${result.text}\n` message += `${result.text}\n`;
} else { } else {
message += `${result.text}\n` message += `${result.text}\n`;
} }
if (result.formats().length > 0) { if (result.formats().length > 0) {
message += `It has following formats: ${result.formats()}\n` message += `It has following formats: ${result.formats()}\n`;
} }
} }
return message return message;
} }
if ( if (
execution.logs.stdout.length > 0 || execution.logs.stdout.length > 0 ||
execution.logs.stderr.length > 0 execution.logs.stderr.length > 0
) { ) {
let message = '' let message = "";
if (execution.logs.stdout.length > 0) { if (execution.logs.stdout.length > 0) {
message += `${execution.logs.stdout.join('\n')}\n` message += `${execution.logs.stdout.join("\n")}\n`;
} }
if (execution.logs.stderr.length > 0) { if (execution.logs.stderr.length > 0) {
message += `${execution.logs.stderr.join('\n')}\n` message += `${execution.logs.stderr.join("\n")}\n`;
} }
return message return message;
} }
return 'There was no output of the execution.' return "There was no output of the execution.";
} },
}), }),
}, },
}); });

View File

@ -188,7 +188,7 @@ export default function Home() {
const models = [ const models = [
{ name: 'Speed', description: 'High speed, but lower quality.', details: '(Anthropic/Claude-3-Haiku)', icon: FastForward }, { name: 'Speed', description: 'High speed, but lower quality.', details: '(Anthropic/Claude-3-Haiku)', icon: FastForward },
{ name: 'Quality (Claude)', description: 'High quality generation.', details: '(Anthropic/Claude-3.5-Sonnet)', icon: Sparkles }, { name: 'Quality', description: 'High quality generation.', details: '(Anthropic/Claude-3.5-Sonnet)', icon: Sparkles },
]; ];
const handleModelChange = (value: string) => { const handleModelChange = (value: string) => {
@ -855,7 +855,7 @@ export default function Home() {
className={`flex items-center font-semibold ${models.find((model) => model.name === selectedModel)?.name.includes('Quality') ? 'text-purple-500' : 'text-green-500'} focus:outline-none focus:ring-0 `} className={`flex items-center font-semibold ${models.find((model) => model.name === selectedModel)?.name.includes('Quality') ? 'text-purple-500' : 'text-green-500'} focus:outline-none focus:ring-0 `}
> >
{selectedModel === 'Speed' && <FastForward className="w-5 h-5 mr-2" />} {selectedModel === 'Speed' && <FastForward className="w-5 h-5 mr-2" />}
{(selectedModel === 'Quality (Claude)') && <Sparkles className="w-5 h-5 mr-2" />} {(selectedModel === 'Quality') && <Sparkles className="w-5 h-5 mr-2" />}
{selectedModel} {selectedModel}
<ChevronDown className={`w-5 h-5 ml-2 transform transition-transform ${isModelSelectorOpen ? 'rotate-180' : ''}`} /> <ChevronDown className={`w-5 h-5 ml-2 transform transition-transform ${isModelSelectorOpen ? 'rotate-180' : ''}`} />
</button> </button>