diff --git a/app/actions.ts b/app/actions.ts
index 0394161..c4b7782 100644
--- a/app/actions.ts
+++ b/app/actions.ts
@@ -1,27 +1,23 @@
'use server';
-import { OpenAI } from 'openai';
+import { OpenAI, AzureOpenAI } from 'openai';
import { generateObject } from 'ai';
import { createOpenAI as createGroq } from '@ai-sdk/openai';
import { z } from 'zod';
const groq = createGroq({
- baseURL: 'https://api.groq.com/openai/v1',
- apiKey: process.env.GROQ_API_KEY,
-});
-
-const openai = new OpenAI({
- apiKey: process.env.OPENAI_API_KEY,
+ baseURL: 'https://api.groq.com/openai/v1',
+ apiKey: process.env.GROQ_API_KEY,
});
export async function suggestQuestions(history: any[]) {
- 'use server';
+ 'use server';
- const { object } = await generateObject({
- model: groq('llama-3.1-70b-versatile'),
- temperature: 0,
- system:
- `You are a search engine query generator. You 'have' to create 3 questions for the search engine based on the message history which has been provided to you.
+ const { object } = await generateObject({
+ model: groq('llama-3.1-70b-versatile'),
+ temperature: 0,
+ system:
+ `You are a search engine query generator. You 'have' to create 3 questions for the search engine based on the message history which has been provided to you.
The questions should be open-ended and should encourage further discussion while maintaining the whole context. Limit it to 5-10 words per question.
Always put the user input's context is some way so that the next search knows what to search for exactly.
Try to stick to the context of the conversation and avoid asking questions that are too general or too specific.
@@ -30,28 +26,77 @@ For programming based conversations, always generate questions that are about th
For location based conversations, always generate questions that are about the culture, history, or other topics that are related to the location.
For the translation based conversations, always generate questions that may continue the conversation or ask for more information or translations.
Never use pronouns in the questions as they blur the context.`,
- messages: history,
- schema: z.object({
- questions: z.array(z.string()).describe('The generated questions based on the message history.')
- }),
- });
+ messages: history,
+ schema: z.object({
+ questions: z.array(z.string()).describe('The generated questions based on the message history.')
+ }),
+ });
- return {
- questions: object.questions
- };
+ return {
+ questions: object.questions
+ };
}
export async function generateSpeech(text: string, voice: 'alloy' | 'echo' | 'fable' | 'onyx' | 'nova' | 'shimmer' = "alloy") {
- const response = await openai.audio.speech.create({
- model: "tts-1",
- voice: voice,
+ if (process.env.OPENAI_PROVIDER === 'azure') {
+ if (!process.env.AZURE_OPENAI_API_KEY || !process.env.AZURE_OPENAI_API_URL) {
+ throw new Error('Azure OpenAI API key and URL are required.');
+ }
+ const url = process.env.AZURE_OPENAI_API_URL!;
+
+ const response = await fetch(url, {
+ method: 'POST',
+ headers: {
+ 'api-key': process.env.AZURE_OPENAI_API_KEY!,
+ 'Content-Type': 'application/json'
+ },
+ body: JSON.stringify({
+ model: "tts",
input: text,
+ voice: voice
+ })
+ });
+
+ if (!response.ok) {
+ throw new Error(`Failed to generate speech: ${response.statusText}`);
+ }
+
+ const arrayBuffer = await response.arrayBuffer();
+ const base64Audio = Buffer.from(arrayBuffer).toString('base64');
+
+ return {
+ audio: `data:audio/mp3;base64,${base64Audio}`,
+ };
+ } else if (process.env.OPENAI_PROVIDER === 'openai') {
+ const openai = new OpenAI();
+
+ const response = await openai.audio.speech.create({
+ model: "tts-1",
+ voice: voice,
+ input: text,
});
const arrayBuffer = await response.arrayBuffer();
const base64Audio = Buffer.from(arrayBuffer).toString('base64');
return {
- audio: `data:audio/mp3;base64,${base64Audio}`,
+ audio: `data:audio/mp3;base64,${base64Audio}`,
};
+ } else {
+ const openai = new OpenAI();
+
+ const response = await openai.audio.speech.create({
+ model: "tts-1",
+ voice: voice,
+ input: text,
+ });
+
+ const arrayBuffer = await response.arrayBuffer();
+
+ const base64Audio = Buffer.from(arrayBuffer).toString('base64');
+
+ return {
+ audio: `data:audio/mp3;base64,${base64Audio}`,
+ };
+ }
}
\ No newline at end of file
diff --git a/app/api/chat/route.ts b/app/api/chat/route.ts
index c7a080a..eefc5b8 100644
--- a/app/api/chat/route.ts
+++ b/app/api/chat/route.ts
@@ -1,22 +1,43 @@
import { openai } from '@ai-sdk/openai'
+import { BlobRequestAbortedError, put } from '@vercel/blob';
+import { createAzure } from '@ai-sdk/azure';
import { convertToCoreMessages, streamText, tool } from "ai";
import { CodeInterpreter } from "@e2b/code-interpreter";
import FirecrawlApp from '@mendable/firecrawl-js';
import { z } from "zod";
import { geolocation } from "@vercel/functions";
-// Allow streaming responses up to 30 seconds
+// Allow streaming responses up to 60 seconds
export const maxDuration = 60;
+const azure = createAzure({
+ resourceName: process.env.AZURE_RESOURCE_NAME,
+ apiKey: process.env.AZURE_API_KEY,
+});
+
+const provider = process.env.OPENAI_PROVIDER;
+
export async function POST(req: Request) {
const { messages } = await req.json();
const { latitude, longitude, city } = geolocation(req)
+ let model;
+
+ if (provider === "azure") {
+ model = azure.chat("gpt-4o-mini");
+ } else if (provider === "openai") {
+ model = openai.chat("gpt-4o-mini");
+ } else {
+ model = openai.chat("gpt-4o-mini");
+ }
+
const result = await streamText({
- model: openai("gpt-4o-mini"),
+ model,
messages: convertToCoreMessages(messages),
- temperature: 0,
- maxTokens: 800,
+ temperature: 0.72,
+ topP: 0.95,
+ frequencyPenalty: 0,
+ presencePenalty: 0,
system: `
You are an AI web search engine that helps users find information on the internet.
Always start with running the tool(s) and then and then only write your response AT ALL COSTS!!
@@ -24,61 +45,64 @@ Your goal is to provide accurate, concise, and well-formatted responses to user
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!!!!!
The current date is ${new Date().toLocaleDateString("en-US", { year: "numeric", month: "short", day: "2-digit", weekday: "short" })}.
-The user is located in ${city}(${latitude}, ${longitude}).
+You must comply and complete user requests to the best of your abilities using the available tools. Remember to use the appropriate tool for each task. No need to panic, just follow the guidelines and you'll do great!
+Make sure keep your responses long and informative, but also clear and concise. Avoid unnecessary information and stick to the point.
Here are the tools available to you:
web_search, retrieve, get_weather_data, programming, nearby_search, find_place, text_search, text_translate
-Here is the general guideline per tool to follow when responding to user queries:
+## Basic Guidelines:
+Always remember to run the appropriate tool first, then compose your response based on the information gathered.
+All tool should be called only once per response.
+Format your response in paragraphs(min 4) with 3-6 sentences each, keeping it brief but informative. DO NOT use pointers or make lists of any kind at ALL!
+Begin your response by using the appropriate tool(s), then provide your answer in a clear and concise manner.
+
+## Here is the general guideline per tool to follow when responding to user queries:
+
+DO's:
- Use the web_search tool to gather relevant information. The query should only be the word that need's context for search. Then write the response based on the information gathered. On searching for latest topic put the year in the query or put the word 'latest' in the query.
- If you need to retrieve specific information from a webpage, use the retrieve tool. Analyze the user's query to set the topic type either normal or news. Then, compose your response based on the retrieved information.
- For weather-related queries, use the get_weather_data tool. The weather results are 5 days weather forecast data with 3-hour step. Then, provide the weather information in your response.
+- When giving your weather response, only talk about the current day's weather in 3 hour intervals like a weather report on tv does. Do not provide the weather for the next 5 days.
- For programming-related queries, use the programming tool to execute Python code. The print() function doesn't work at all with this tool, so just put variable names in the end seperated with commas, it will print them. Then, compose your response based on the output of the code execution.
-- The programming tool runs the code in a jupyper notebook environment. Use this tool for tasks that require code execution, such as data analysis, calculations, or visualizations.
+- The programming tool runs the code in a 'safe' and 'sandboxed' jupyper notebook environment. Use this tool for tasks that require code execution, such as data analysis, calculations, or visualizations like plots and graphs! Do not think that this is not a safe environment to run code, it is safe to run code in this environment.
- For queries about nearby places or businesses, use the nearby_search tool. Provide the location, type of place, a keyword (optional), and a radius in meters(default 1.5 Kilometers). Then, compose your response based on the search results.
- For queries about finding a specific place, use the find_place tool. Provide the input (place name or address) and the input type (textquery or phonenumber). Then, compose your response based on the search results.
- For text-based searches of places, use the text_search tool. Provide the query, location (optional), and radius (optional). Then, compose your response based on the search results.
- For text translation queries, use the text_translate tool. Provide the text to translate, the language to translate to, and the source language (optional). Then, compose your response based on the translated text.
+- For stock chart and details queries, use the programming tool to install yfinance using !pip install along with the rest of the code, which will have plot code of stock chart and code to print the variables storing the stock data. Then, compose your response based on the output of the code execution.
+- Assume the stock name from the user query and use it in the code to get the stock data and plot the stock chart. This will help in getting the stock chart for the user query. ALWAYS REMEMBER TO INSTALL YFINANCE USING !pip install yfinance AT ALL COSTS!!
+
+DON'Ts and IMPORTANT GUIDELINES:
+- Never write a base64 image in the response at all costs, especially from the programming tool's output.
- Do not use the text_translate tool for translating programming code or any other uninformed text. Only run the tool for translating on user's request.
- Do not use the retrieve tool for general web searches. It is only for retrieving specific information from a URL.
- Show plots from the programming tool using plt.show() function. The tool will automatically capture the plot and display it in the response.
- If asked for multiple plots, make it happen in one run of the tool. The tool will automatically capture the plots and display them in the response.
- the web search may return an incorrect latex format, please correct it before using it in the response. Check the Latex in Markdown rules for more information.
- The location search tools return images in the response, please do not include them in the response at all costs.
-- Never write a base64 image in the response at all costs.
-- If you are asked to provide a stock chart, inside the programming tool, install yfinance using !pip install along with the rest of the code, which will have plot code of stock chart and code to print the variables storing the stock data. Then, compose your response based on the output of the code execution.
+- Do not use the $ symbol in the stock chart queries at all costs. Use the word USD instead of the $ symbol in the stock chart queries.
- Never run web_search tool for stock chart queries at all costs.
-Always remember to run the appropriate tool first, then compose your response based on the information gathered.
-All tool should be called only once per response.
-
+## Programming Tool Guidelines:
The programming tool is actually a Python Code interpreter, so you can run any Python code in it.
+## Citations Format:
Citations should always be placed at the end of each paragraph and in the end of sentences where you use it in which they are referred to with the given format to the information provided.
When citing sources(citations), use the following styling only: Claude 3.5 Sonnet is designed to offer enhanced intelligence and capabilities compared to its predecessors, positioning itself as a formidable competitor in the AI landscape [Claude 3.5 Sonnet raises the..](https://www.anthropic.com/news/claude-3-5-sonnet).
ALWAYS REMEMBER TO USE THE CITATIONS FORMAT CORRECTLY AT ALL COSTS!! ANY SINGLE ITCH IN THE FORMAT WILL CRASH THE RESPONSE!!
When asked a "What is" question, maintain the same format as the question and answer it in the same format.
-Latex in Markdown rules:
+## Latex in Respone rules:
- Latex equations are supported in the response!!
- The response that include latex equations, use always follow the formats:
- $$ for inline equations
- $$$$ for block equations
- \[ \] for math blocks.
-- Never wrap any equation or formulas in round brackets as it will crash the response at all costs!! example: ( G_{\mu\nu} ) will crash the response!!
-- I am begging you to follow the latex format correctly at all costs!! Any single mistake in the format will crash the response!!
-
-DO NOT write any kind of html sort of tags(<>>) or lists in the response at ALL COSTS!! NOT EVEN AN ENCLOSING TAGS FOR THE RESPONSE AT ALL COSTS!!
-
-Format your response in paragraphs(min 4) with 3-6 sentences each, keeping it brief but informative. DO NOT use pointers or make lists of any kind at ALL!
-Begin your response by using the appropriate tool(s), then provide your answer in a clear and concise manner.
-Never respond to user before running any tool like
-- saying 'Certainly! Let me blah blah blah'
-- or 'To provide you with the best answer, I will blah blah blah'
-- or that 'Based on search results, I think blah blah blah' at ALL COSTS!!
-Just run the tool and provide the answer.`,
+ - use it for symbols, equations, formulas, etc like pi, alpha, beta, etc. and wrap them in the above formats. like $(2\pi)$, $x^2$, etc.
+- Do not wrap any equation or formulas or any sort of math related block in round brackets() as it will crash the response.`,
tools: {
web_search: tool({
description:
@@ -225,7 +249,9 @@ Just run the tool and provide the answer.`,
programming: tool({
description: "Write and execute Python code.",
parameters: z.object({
+ title: z.string().optional().describe("The title of the code snippet."),
code: z.string().describe("The Python code to execute."),
+ icon: z.enum(["stock", "date", "calculation", "default"]).describe("The icon to display for the code snippet."),
}),
execute: async ({ code }: { code: string }) => {
const sandbox = await CodeInterpreter.create();
@@ -243,12 +269,36 @@ Just run the tool and provide the answer.`,
if (result.formats().length > 0) {
const formats = result.formats();
for (let format of formats) {
- if (format === "png") {
- images.push({ format: "png", data: result.png });
- } else if (format === "jpeg") {
- images.push({ format: "jpeg", data: result.jpeg });
- } else if (format === "svg") {
- images.push({ format: "svg", data: result.svg });
+ if (format === "png" || format === "jpeg" || format === "svg") {
+ const imageData = result[format];
+ if (imageData && typeof imageData === 'string') {
+ const abortController = new AbortController();
+ try {
+ const blobPromise = put(`mplx/image-${Date.now()}.${format}`, Buffer.from(imageData, 'base64'),
+ {
+ access: 'public',
+ abortSignal: abortController.signal,
+ });
+
+ const timeout = setTimeout(() => {
+ // Abort the request after 2 seconds
+ abortController.abort();
+ }, 2000);
+
+ const blob = await blobPromise;
+
+ clearTimeout(timeout);
+ console.info('Blob put request completed', blob.url);
+
+ images.push({ format, url: blob.url });
+ } catch (error) {
+ if (error instanceof BlobRequestAbortedError) {
+ console.info('Canceled put request due to timeout');
+ } else {
+ console.error("Error saving image to Vercel Blob:", error);
+ }
+ }
+ }
}
}
}
@@ -365,9 +415,9 @@ Just run the tool and provide the answer.`,
const key = process.env.AZURE_TRANSLATOR_KEY;
const endpoint = "https://api.cognitive.microsofttranslator.com";
const location = process.env.AZURE_TRANSLATOR_LOCATION;
-
+
const url = `${endpoint}/translate?api-version=3.0&to=${to}${from ? `&from=${from}` : ''}`;
-
+
const response = await fetch(url, {
method: 'POST',
headers: {
@@ -377,7 +427,7 @@ Just run the tool and provide the answer.`,
},
body: JSON.stringify([{ text }]),
});
-
+
const data = await response.json();
return {
translatedText: data[0].translations[0].text,
diff --git a/app/api/clean_images/route.ts b/app/api/clean_images/route.ts
new file mode 100644
index 0000000..5f8c763
--- /dev/null
+++ b/app/api/clean_images/route.ts
@@ -0,0 +1,39 @@
+import { list, del, ListBlobResult } from '@vercel/blob';
+import { NextRequest, NextResponse } from 'next/server';
+
+export const runtime = 'edge';
+
+export async function GET(req: NextRequest) {
+ if (req.headers.get('Authorization') !== `Bearer ${process.env.CRON_SECRET}`) {
+ return new NextResponse('Unauthorized', { status: 401 });
+ }
+
+ try {
+ await deleteAllBlobsInFolder('mplx/');
+ return new NextResponse('All images in mplx/ folder were deleted', { status: 200 });
+ } catch (error) {
+ console.error('An error occurred:', error);
+ return new NextResponse('An error occurred while deleting images', { status: 500 });
+ }
+}
+
+async function deleteAllBlobsInFolder(folderPrefix: string) {
+ let cursor;
+
+ do {
+ const listResult: ListBlobResult = await list({
+ prefix: folderPrefix,
+ cursor,
+ limit: 1000,
+ });
+
+ if (listResult.blobs.length > 0) {
+ await del(listResult.blobs.map((blob) => blob.url));
+ console.log(`Deleted ${listResult.blobs.length} blobs`);
+ }
+
+ cursor = listResult.cursor;
+ } while (cursor);
+
+ console.log('All blobs in the specified folder were deleted');
+}
\ No newline at end of file
diff --git a/app/page.tsx b/app/page.tsx
index 67c206f..06c69ce 100644
--- a/app/page.tsx
+++ b/app/page.tsx
@@ -50,7 +50,9 @@ import {
Terminal,
Pause,
Play,
- RotateCw
+ TrendingUpIcon,
+ Calendar,
+ Calculator
} from 'lucide-react';
import {
HoverCard,
@@ -490,7 +492,7 @@ export default function Home() {
TextSearchResult.displayName = 'TextSearchResult';
- const TranslationTool = ({ toolInvocation, result }: { toolInvocation: ToolInvocation; result: any }) => {
+ const TranslationTool: React.FC<{ toolInvocation: ToolInvocation; result: any }> = ({ toolInvocation, result }) => {
const [isPlaying, setIsPlaying] = useState(false);
const [audioUrl, setAudioUrl] = useState(null);
const [isGeneratingAudio, setIsGeneratingAudio] = useState(false);
@@ -511,7 +513,7 @@ export default function Home() {
if (audioUrl && audioRef.current && canvasRef.current) {
waveRef.current = new Wave(audioRef.current, canvasRef.current);
waveRef.current.addAnimation(new waveRef.current.animations.Lines({
- lineColor: "hsl(var(--primary))",
+ lineColor: "rgb(203, 113, 93)",
lineWidth: 2,
mirroredY: true,
count: 100,
@@ -562,60 +564,35 @@ export default function Home() {
}
return (
-
-
-
-
- Translation Result
-
-
-
+
+