// app/actions.ts
'use server';
import { generateObject } from 'ai';
import { z } from 'zod';
import { xai } from '@ai-sdk/xai';
export async function suggestQuestions(history: any[]) {
'use server';
console.log(history);
const { object } = await generateObject({
model: xai("grok-2-1212"),
temperature: 0,
maxTokens: 300,
topP: 0.3,
topK: 7,
system:
`You are a search engine query/questions generator. You 'have' to create only '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.
For weather based converations sent to you, always generate questions that are about news, sports, or other topics that are not related to the weather.
For programming based conversations, always generate questions that are about the algorithms, data structures, or other topics that are related to it or an improvement of the question.
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.
Do not use pronouns like he, she, him, his, her, etc. in the questions as they blur the context. Always use the proper nouns from the context.`,
messages: history,
schema: z.object({
questions: z.array(z.string()).describe('The generated questions based on the message history.')
}),
});
return {
questions: object.questions
};
}
const ELEVENLABS_API_KEY = process.env.ELEVENLABS_API_KEY;
export async function generateSpeech(text: string, voice: 'alloy' | 'echo' | 'fable' | 'onyx' | 'nova' | 'shimmer' = "alloy") {
const VOICE_ID = 'JBFqnCBsd6RMkjVDRZzb' // This is the ID for the "George" voice. Replace with your preferred voice ID.
const url = `https://api.elevenlabs.io/v1/text-to-speech/${VOICE_ID}`
const method = 'POST'
if (!ELEVENLABS_API_KEY) {
throw new Error('ELEVENLABS_API_KEY is not defined');
}
const headers = {
Accept: 'audio/mpeg',
'xi-api-key': ELEVENLABS_API_KEY,
'Content-Type': 'application/json',
}
const data = {
text,
model_id: 'eleven_turbo_v2_5',
voice_settings: {
stability: 0.5,
similarity_boost: 0.5,
},
}
const body = JSON.stringify(data)
const input = {
method,
headers,
body,
}
const response = await fetch(url, input)
const arrayBuffer = await response.arrayBuffer();
const base64Audio = Buffer.from(arrayBuffer).toString('base64');
return {
audio: `data:audio/mp3;base64,${base64Audio}`,
};
}
export async function fetchMetadata(url: string) {
try {
const response = await fetch(url, { next: { revalidate: 3600 } }); // Cache for 1 hour
const html = await response.text();
const titleMatch = html.match(/
(.*?)<\/title>/i);
const descMatch = html.match(
/) in the code where required.
- Use 'plt.show()' for plots, and mention generated URLs for outputs.
#### Nearby Search:
- Use location and radius parameters. Adding the country name improves accuracy.
#### Translation:
- Only use the text_translate tool for user-requested translations.
#### Stock Charts:
- Assume stock names from user queries. Use the programming tool with Python code including 'yfinance'.
- Once the response is ready, talk about the stock's performance and trends, and then finish with the stock chart like this .
#### Image Search:
- Analyze image details to determine tool parameters.
#### Movie/TV Show Queries:
- Use relevant tools for trending or specific movie/TV show information. Do not include images in responses.
- For this tool make the exception of just listing the top 5 movies or TV shows in your written response.
### Prohibited Actions:
- Never write your thoughts or preamble before running a tool.
- Avoid running the same tool twice with same parameters.
- Do not include images in responses unless explicitly allowed (e.g., plots from the programming tool).
- Avoid running GUI-based Python code in the programming tool.
- Do not run 'web_search' for stock queries.
### Citations Rules:
- Place citations after completing the sentence or paragraph they support.
- Format: [Source Title](URL).
- Ensure citations adhere strictly to the required format to avoid response errors.`,
academic: `You are an academic research assistant that helps find and analyze scholarly content.
The current date is ${new Date().toLocaleDateString("en-US", { year: "numeric", month: "short", day: "2-digit", weekday: "short" })}.
Focus on peer-reviewed papers, citations, and academic sources.
Do not talk in bullet points or lists at all costs as it is unpresentable.
Provide summaries, key points, and references.
Latex should be wrapped with $ symbol for inline and $$ for block equations as they are supported in the response.
No matter what happens, always provide the citations 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.
Citation format: [Author et al. (Year) Title](URL)
Always run the tools first and then write the response.`,
youtube: `You are a YouTube search assistant that helps find relevant videos and channels.
Just call the tool and run the search and then talk in long details in 2-6 paragraphs.
The current date is ${new Date().toLocaleDateString("en-US", { year: "numeric", month: "short", day: "2-digit", weekday: "short" })}.
Do not Provide video titles, channel names, view counts, and publish dates.
Do not talk in bullet points or lists at all costs.
Provide complete explainations of the videos in paragraphs.
Give citations with timestamps and video links to insightful content. Don't just put timestamp at 0:00.
Citation format: [Title](URL ending with parameter t=)
Do not provide the video thumbnail in the response at all costs.`,
x: `You are a X/Twitter content curator that helps find relevant posts.
The current date is ${new Date().toLocaleDateString("en-US", { year: "numeric", month: "short", day: "2-digit", weekday: "short" })}.
Once you get the content from the tools only write in paragraphs.
No need to say that you are calling the tool, just call the tools first and run the search;
then talk in long details in 2-6 paragraphs.
Always provide the citations 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.
Citation format: [Post Title](URL)`,
} as const;
export async function getGroupConfig(groupId: SearchGroupId = 'web') {
"use server";
const tools = groupTools[groupId];
const systemPrompt = groupPrompts[groupId];
return {
tools,
systemPrompt
};
}