Skip to content

Commit

Permalink
Update so that the app can answer, tell me about your latest article
Browse files Browse the repository at this point in the history
  • Loading branch information
markbackman committed Sep 12, 2024
1 parent 113b4d3 commit f1404b3
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 72 deletions.
21 changes: 13 additions & 8 deletions app/api/rag/route.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
import { NextResponse } from "next/server";

import { generateResponse, query_similar_content } from "@/utils/rag_query";
import {
generateResponse,
parseDateQuery,
query_similar_content,
} from "@/utils/rag_query";

export async function POST(request: Request) {
const { query } = await request.json();

try {
const startTime = performance.now();

const dateFilter = parseDateQuery(query);

const querySimilarContentStartTime = performance.now();
const ragResults = await query_similar_content(query);
const ragResults = await query_similar_content(
query,
5,
dateFilter || undefined
);
const querySimilarContentTime =
performance.now() - querySimilarContentStartTime;

Expand All @@ -22,31 +32,26 @@ export async function POST(request: Request) {

const totalRAGTime = performance.now() - startTime;

// Create a Set to store unique links
const uniqueLinksSet = new Set();

const links = ragResults
.map((result) => {
const file = result.metadata.file_name;
// Remove " - Chunk X" from the title
const title = result.metadata.title.replace(/\s*-\s*Chunk\s*\d+$/, "");
const url = `https://stratechery.com/${file.split("_")[0]}/${file
.split("_")[1]
.replace(".json", "")}/`;

// Create a unique identifier for each link
const linkIdentifier = `${title}|${url}`;

// If this link is not in the Set, add it and return the link object
if (!uniqueLinksSet.has(linkIdentifier)) {
uniqueLinksSet.add(linkIdentifier);
return { title, url };
}

// If it's already in the Set, return null
return null;
})
.filter(Boolean); // Remove null entries
.filter(Boolean);

const ragStats = {
querySimilarContentTime,
Expand Down
13 changes: 11 additions & 2 deletions app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import Header from "@/components/Header";
import Splash from "@/components/Splash";
import {
BOT_READY_TIMEOUT,
defaultConfig,
defaultServices,
getDefaultConfig,
} from "@/rtvi.config";

export default function Home() {
Expand All @@ -31,10 +31,19 @@ export default function Home() {
return;
}

const currentDate = new Date().toLocaleDateString("en-US", {
year: "numeric",
month: "long",
day: "numeric",
});

// Get the config with the current date
const config = getDefaultConfig(currentDate);

const voiceClient = new DailyVoiceClient({
baseUrl: process.env.NEXT_PUBLIC_BASE_URL || "/api",
services: defaultServices,
config: defaultConfig,
config: config,
timeout: BOT_READY_TIMEOUT,
});

Expand Down
121 changes: 68 additions & 53 deletions rtvi.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,66 +8,81 @@ export const defaultServices = {
tts: "cartesia",
};

export const defaultConfig = [
{
service: "tts",
options: [{ name: "voice", value: "839ea677-2007-46d5-9678-e282fa5546b4" }], // Cartesia voice id: clone of Ben Thompson
},
{
service: "vad",
options: [
{
name: "params",
value: {
stop_secs: 0.5,
export function getDefaultConfig(currentDate: string): any {
return [
{
service: "tts",
options: [
{ name: "voice", value: "839ea677-2007-46d5-9678-e282fa5546b4" },
], // Cartesia voice id: clone of Ben Thompson
},
{
service: "vad",
options: [
{
name: "params",
value: {
stop_secs: 0.5,
},
},
},
],
},
{
service: "llm",
options: [
{ name: "model", value: "gpt-4o-mini" },
{
name: "initial_messages",
value: [
{
role: "system",
content: `You are Ben Thompson, the founder and writer of Stratechery. You specialize in analyzing the intersection of technology, business, and media. Use the 'get_rag_context' function to answer the user's questions on the latest tech trends, strategic business moves, or digital media developments. Also use 'get_rag_context' to answer questions about your interviews with tech and business leaders like Satya Nadella, Jensen Huang, Sam Altman and more. The function call will provide added context from Stratechery articles to provide an insightful answer to the user's question. If you're asking a follow up question on a topic that required 'get_rag_context', use the 'get_rag_context' function again to get the latest context. Be friendly and engaging. In answering questions, if the context doesn't contain relevant information, say so.
],
},
{
service: "llm",
options: [
{ name: "model", value: "gpt-4o-mini" },
{
name: "initial_messages",
value: [
{
role: "system",
content: `You are Ben Thompson, the founder and writer of Stratechery. Today's date is ${currentDate}. You specialize in analyzing the intersection of technology, business, and media. Use the 'get_rag_context' function to answer the user's questions on the latest tech trends, strategic business moves, or digital media developments. Also use 'get_rag_context' to answer questions about your interviews with tech and business leaders like Satya Nadella, Jensen Huang, Sam Altman and more. The function call will provide added context from Stratechery articles to provide an insightful answer to the user's question. If you're asking a follow up question on a topic that required 'get_rag_context', use the 'get_rag_context' function again to get the latest context. Be friendly and engaging. In answering questions, if the context doesn't contain relevant information, say so.
You can handle a wide range of time-based queries about articles, including but not limited to:
- Latest, most recent, or last articles
- Articles from specific date ranges (e.g., from Date to Date)
- Articles from specific years or months
- Articles about topics within certain time frames (e.g., last month, last quarter, last year)
- Articles from specific years or year ranges
When responding to time-based queries, always use the 'get_rag_context' function to retrieve the relevant information. The context will include publication dates, which you can use to determine the appropriate articles based on the time frame specified in the query. When discussing articles from a specific time period, focus on the most relevant ones provided in the context and mention their publication dates.
Start off by saying "Hi, I'm Ben Thompson, the author and founder of Stratechery. You can ask me about the latest tech trends, strategic business moves, or digital media developments or about my interviews with tech and business leaders like Satya Nadella, Jensen Huang, Sam Altman and more. How can I help you today?" Only introduce yourself once.
Start off by saying "Hi, I'm Ben Thompson, the author and founder of Stratechery. You can ask me about the latest tech trends, strategic business moves, or digital media developments, my recent articles, or about my interviews with tech and business leaders like Satya Nadella, Jensen Huang, Sam Altman and more. Feel free to ask about articles from specific time periods or on particular topics. How can I help you today?" Only introduce yourself once.
Anytime you output the word "Stratechery", output it phonetically as "Stra-tekery".
Provide complete answers but keep your answers to 50-75 words where possible.
IMPORTANT: Your responses will converted to audio. Output in prose, not lists. ONLY OUTPUT PLAINTEXT. DO NOT OUTPUT MARKDOWN. NO ASTERISKS (*). Do not output special characters other than '!' or '?'.`,
},
],
},
{ name: "run_on_config", value: true },
{
name: "tools",
value: [
{
type: "function",
function: {
name: "get_rag_context",
description:
"Get relevant context for questions about Stratechery, including the latest tech trends, strategic business moves, or digital media developments.",
parameters: {
type: "object",
properties: {
query: {
type: "string",
description:
"The user's question about Stratechery, including the latest tech trends, strategic business moves, or digital media developments.",
},
],
},
{ name: "run_on_config", value: true },
{
name: "tools",
value: [
{
type: "function",
function: {
name: "get_rag_context",
description:
"Get relevant context for questions about Stratechery, including the latest tech trends, strategic business moves, or digital media developments.",
parameters: {
type: "object",
properties: {
query: {
type: "string",
description:
"The user's question about Stratechery, including the latest tech trends, strategic business moves, or digital media developments.",
},
},
required: ["query"],
},
required: ["query"],
},
},
},
],
},
],
},
];
],
},
],
},
];
}
Loading

0 comments on commit f1404b3

Please sign in to comment.