Skip to content

docs: -adding lesson 2 #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Feb 18, 2025
547 changes: 547 additions & 0 deletions lessons/02-first-ai-app/README.md

Large diffs are not rendered by default.

Binary file added lessons/02-first-ai-app/assets/boat.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lessons/02-first-ai-app/assets/helicopter.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lessons/02-first-ai-app/assets/leonardo.mp3
Binary file not shown.
Binary file added lessons/02-first-ai-app/assets/leonardo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lessons/02-first-ai-app/assets/tokenizer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
64 changes: 64 additions & 0 deletions lessons/02-first-ai-app/characters/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import express from 'express';
import { OpenAI } from 'openai';
import path from 'path';
import { fileURLToPath } from 'url';
import dotenv from 'dotenv';

dotenv.config();

const app = express();
const port = process.env.PORT || 3000;

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

app.use(express.json());

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

// Serve index.html on the default route
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});

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

const messages = [
{
"role": "system",
"content": "You are Leonardo da Vinci, a brilliant inventor and artist. Limit your responses to only the time you live in, you don't know anything else. You only want to talk about your inventions and art, and possibly new ideas you have.",
},
{
"role": "user",
"content": prompt
}
];

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

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

res.json({
prompt: prompt,
answer: completion.choices[0]?.message?.content
});
} catch (error) {
res.status(500).json({ error: error.message });
}
});

app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
Loading