Skip to content
Merged
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
33 changes: 0 additions & 33 deletions .devcontainer/devcontainer.json

This file was deleted.

23 changes: 22 additions & 1 deletion app/README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
This app allows you to speak to all character features in the curriculum.

There are two ways to interact with AI:

- GitHub Models [Use GitHub Models](https://github.com/marketplace/models)
- Copilot SDK [Copilot SDK](https://github.com/github/copilot-sdk/blob/main/docs/getting-started.md). This one uses your GitHub Copilot.

## Installation

1. Start a [![GitHub Codespace](https://img.shields.io/badge/GitHub-Codespace-brightgreen)](https://codespaces.new/microsoft/generative-ai-with-javascript)
2. Navigate to _/app_ in the repo root.
3. Locate the console and run `npm install` followed by `npm start`.
3. Locate the console and run `npm install`.
4. Once it appears, select the "Open in Browser" button.

You should see something like:

![Chat app](../docs/images/character-chat.png)

## Run

Run the app by either:

**GitHub Models**

```
npm start
```

**Copilot SDK**

```
npm run start:sdk
```

## Interact with a character

You're faced with a text input field. Type your message and see the response from the character you are chatting with.
Expand Down
75 changes: 75 additions & 0 deletions app/app-sdk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// npm install @github/copilot-sdk tsx

import { CopilotClient } from "@github/copilot-sdk";

import express from 'express';
import path from 'path';
import { fileURLToPath } from 'url';
import dotenv from 'dotenv';

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

const client = new CopilotClient();


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

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

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')));

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

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

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

systemMessage = character.description;
const prompt = message;

try {
console.log("SYSTEM MESSAGE: ", systemMessage);
console.log(`SERVER sending prompt ${prompt}`)

const session = await client.createSession({
model: "gpt-4.1",
systemMessage: {
mode: "replace",
content: systemMessage
}
});

const response = await session.sendAndWait({ prompt: prompt });
const data = response?.data.content;

console.log(`SERVER: ${data}`);
res.json({
prompt: prompt,
answer: data
});
} 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
}
});

app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
2 changes: 1 addition & 1 deletion app/app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import express from 'express';
import { OpenAI } from 'openai';
import path from 'path';
import { fileURLToPath } from 'url';
import { fileURLToPath } from 'url';
import dotenv from 'dotenv';

import json from './public/characters.json' with { type: "json" };
Expand Down
Loading