Skip to content
Open

Rag #214

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 22 additions & 24 deletions app/app.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import express from 'express';
import { OpenAI } from 'openai';
import path from 'path';
import { fileURLToPath } from 'url';
import dotenv from 'dotenv';
import dotenv from "dotenv";
import express from "express";
import { OpenAI } from "openai";
import path from "path";
import { fileURLToPath } from "url";

import json from './public/characters.json' with { type: "json" };
dotenv.config();

import json from "./public/characters.json" with { type: "json" };

let systemMessage = json[4].description;
let page = json[4].page;

console.log("SERVER systemMessage: ", systemMessage);
console.log("SERVER page: ", page);

dotenv.config();
// console.log("SERVER systemMessage: ", systemMessage);
// console.log("SERVER page: ", page);

const app = express();
const port = process.env.PORT || 3000;
Expand All @@ -23,16 +23,16 @@ const __dirname = path.dirname(__filename);
app.use(express.json());

// Serve static files from the 'public' directory
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, "public")));

app.locals.delimiters = '{{ }}';
app.locals.delimiters = "{{ }}";

function getCharacterByName(name) {
return json.find(character => character.name === name) || null;
return json.find((character) => character.name === name) || null;
}

// Route to send the prompt
app.post('/send', async (req, res) => {
app.post("/send", async (req, res) => {
const { message, character } = req.body;

systemMessage = character.description;
Expand All @@ -41,13 +41,13 @@ app.post('/send', async (req, res) => {

const messages = [
{
"role": "system",
"content": systemMessage,
role: "system",
content: systemMessage,
},
{
"role": "user",
"content": prompt
}
role: "user",
content: prompt,
},
];

const openai = new OpenAI({
Expand All @@ -56,20 +56,18 @@ app.post('/send', async (req, res) => {
});

try {
console.log(`SERVER sending prompt ${prompt}`)
const completion = await openai.chat.completions.create({
model: 'gpt-4o-mini',
model: "gpt-4o-mini",
messages: messages,
});

console.log(`SERVER: ${completion.choices[0]?.message?.content}`);
res.json({
prompt: prompt,
answer: completion.choices[0]?.message?.content
answer: completion.choices[0]?.message?.content,
});
} catch (error) {
console.error(`Error: ${error.message}`); // Log the error message for debugging
res.status(500).json({ message: 'An unexpected error occurred. Please try again later.' }); // Send a generic error message
res.status(500).json({ message: "An unexpected error occurred. Please try again later." }); // Send a generic error message
}
});

Expand Down
41 changes: 41 additions & 0 deletions app/cipher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { OpenAI } from "openai";
import dotenv from "dotenv";
dotenv.config();

// Create prompt including inputs should include chain of thought

const bootSequence = `nomi`;

const prompt = `Provide the boot sequence for the aerial screw, the text should be mirrored and encoded with a Caesar cipher with a shift of 3. The boot sequence is: ${bootSequence}.`;

// Call the language model with the prompt

const messages = [
{
role: "user",
content: prompt,
},
];

// 2. Create client
// -----------------------------------

const openai = new OpenAI({
baseURL: "https://models.inference.ai.azure.com",
apiKey: process.env.GITHUB_TOKEN,
});

// 3. Send the request
// -----------------------------------

const completion = await openai.chat.completions.create({
model: "gpt-4o-mini",
messages: messages,
});

console.log(`Answer for "${prompt}":`);

// 4. Print the answer
// -----------------------------------

console.log(completion.choices[0]?.message?.content);
47 changes: 47 additions & 0 deletions app/context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import dotenv from "dotenv";
dotenv.config();
import { OpenAI } from "openai";

// Define the context

const messages = [
{
role: "user",
content: "I want to book a trip to Italy.",
},
{
role: "assistant",
content: "Sure, when would you like to go?",
},
{
role: "user",
content: "Next month would be great.",
},
{
role: "assistant",
content: "Got it, where in Italy would you like to visit?",
},
{
role: "user",
content: "I'm thinking of Rome. Tell me more about it.",
},
];

const openai = new OpenAI({
baseURL: "https://models.inference.ai.azure.com",
apiKey: process.env.GITHUB_TOKEN,
});

// 3. Send the request
// -----------------------------------
const completion = await openai.chat.completions.create({
model: "gpt-4o-mini",
messages: messages,
// temperature: 0.7, // Adjust the temperature for creativity, 0.0 (more deterministic) to 1.0 (more creative)
// max_completion_tokens: 512, // Limit the response length.
});

// 4. Print the answer
// -----------------------------------

console.log(completion.choices[0]?.message?.content);
81 changes: 81 additions & 0 deletions app/csv-rag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// This example demonstrates how to use the Retrieval Augmented Generation (RAG)
// to answer questions based on a hybrid car data set.
// The code below reads the CSV file, searches for matches to the user question,
// and then generates a response based on the information found.

import dotenv from "dotenv";
import { fileURLToPath } from "node:url";
import { dirname } from "node:path";
import process from "node:process";
import fs from "node:fs";
import { OpenAI } from "openai";

dotenv.config();
// Change the current working directory to the directory of the script
const __dirname = dirname(fileURLToPath(import.meta.url));
process.chdir(__dirname);

// 1. Ask a question about hybrid cars
// -----------------------------------

const question = `what's the fastest prius`;

// 2. Retriever component: search the data for relevant information
// ----------------------------------------------------------------

// Load CSV data as an array of objects
const rows = fs.readFileSync("./hybrid.csv", "utf8").split("\n");
const columns = rows[0].split(",");

// Search the data using a very naive search
const words = question
.toLowerCase()
.replaceAll(/[.?!()'":,]/g, "")
.split(" ")
.filter((word) => word.length > 2);
const matches = rows.slice(1).filter((row) => words.some((word) => row.toLowerCase().includes(word)));

// Format as a markdown table, since language models understand markdown
const table =
`| ${columns.join(" | ")} |\n` +
`|${columns.map(() => "---").join(" | ")}|\n` +
matches.map((row) => `| ${row.replaceAll(",", " | ")} |\n`).join("");

console.log(`Found ${matches.length} matches:`);
console.log(table);

// 3. Context augmentation: create a combined prompt with the search results
// --------------------------------------------------------------------------

const augmentedPrompt = `
## Instructions
Answer questions about a time period or characters from said time period using only the sources below.
If there's not enough data in provided sources, say that you don't know.
Be brief and straight to the point.

## Sources
${table}

## Question
${question}
`;

// 4. Generator component: use the search results to generate a response
// ---------------------------------------------------------------------

const openai = new OpenAI({
baseURL: "https://models.inference.ai.azure.com",
apiKey: process.env.GITHUB_TOKEN,
});

const chunks = await openai.chat.completions.create({
model: "gpt-4o-mini",
messages: [{ role: "user", content: augmentedPrompt }],
stream: true,
});

console.log(`Answer for "${question}":`);

for await (const chunk of chunks) {
process.stdout.write(chunk.choices[0].delta.content ?? "");
}
Loading