Skip to content

Commit 8cf92ef

Browse files
Merge pull request #23 from appwrite/feat-v1-prompt
feat: v1 prompt
2 parents 4b277b7 + 92b5233 commit 8cf92ef

File tree

5 files changed

+43
-24
lines changed

5 files changed

+43
-24
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ node_modules
33
.vscode
44
.env
55
venv
6-
sources
6+
sources
7+
.DS_Store

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ The server exposes a POST endpoint at `/`. The endpoint expects a raw text body
4141
Use cURL to test the server, for example:
4242

4343
```bash
44-
curl -X POST -d "How do I create a user?" http://localhost:3000
44+
curl -X POST -H "Content-Type: application/json" -d "{\"prompt\": \"How do I create a new user?\"}" http://localhost:3000/v1/models/assistant/prompt
4545
```
4646

4747
## Contributing

scripts/test-prompts.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { writeFile, mkdir } from "fs/promises";
22

3-
const SERVER_URL = "http://localhost:3003/";
3+
const SERVER_URL = "http://localhost:3003/v1/models/assistant/prompt";
44
const PROMPTS = [
55
"How do you add a custom domain in the console?",
66
"Show me how I can set up database collections and documents.",

src/embeddings.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,22 @@ export const intializeDocumentRetriever = async () => {
1515
return vectorStore.asRetriever(5);
1616
};
1717

18-
export const getChain = async (onToken) => {
18+
export const getOpenAIChat = async (onToken) =>
19+
new OpenAIChat({
20+
modelName: "gpt-4o",
21+
openAIApiKey: process.env._APP_ASSISTANT_OPENAI_API_KEY,
22+
temperature: 0.3,
23+
maxTokens: 1000,
24+
streaming: true,
25+
callbacks: [
26+
{
27+
handleLLMNewToken: onToken,
28+
},
29+
],
30+
});
31+
32+
export const getRagChain = async (onToken) => {
1933
return loadQAStuffChain(
20-
new OpenAIChat({
21-
modelName: "gpt-4o",
22-
openAIApiKey: process.env._APP_ASSISTANT_OPENAI_API_KEY,
23-
temperature: 0.3,
24-
maxTokens: 1000,
25-
streaming: true,
26-
callbacks: [
27-
{
28-
handleLLMNewToken: onToken,
29-
},
30-
],
31-
})
34+
(await getOpenAIChat(onToken)),
3235
);
3336
};

src/main.js

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import bodyParser from "body-parser";
33
import cors from "cors";
44
import express from "express";
55
import {
6-
getChain,
6+
getRagChain,
7+
getOpenAIChat,
78
intializeDocumentRetriever as initializeRetriever,
9+
810
} from "./embeddings.js";
911

1012
const app = express();
@@ -20,9 +22,9 @@ let retriever = null;
2022

2123
const port = 3003;
2224

23-
const DEFAULT_SYSTEM_PROMPT = "You are an AI chat bot with information about Appwrite documentation. You need to help developers answer Appwrite related questions only. You will be given an input and you need to respond with the appropriate answer, using information confirmed with Appwrite documentation and reference pages. If applicable, show code examples. Code examples should use the Node and Web Appwrite SDKs unless otherwise specified.";
25+
const SYSTEM_PROMPT = "You are an AI chat bot with information about Appwrite documentation. You need to help developers answer Appwrite related questions only. You will be given an input and you need to respond with the appropriate answer, using information confirmed with Appwrite documentation and reference pages. If applicable, show code examples. Code examples should use the Node and Web Appwrite SDKs unless otherwise specified.";
2426

25-
app.post("/", async (req, res) => {
27+
app.post("/v1/models/assistant/prompt", async (req, res) => {
2628
if (!retriever) {
2729
res.status(500).send("Search index not initialized");
2830
return;
@@ -32,18 +34,16 @@ app.post("/", async (req, res) => {
3234
const decoder = new TextDecoder();
3335
const text = decoder.decode(req.body);
3436

35-
let { prompt, systemPrompt } = JSON.parse(text);
36-
const templated = `${systemPrompt ?? DEFAULT_SYSTEM_PROMPT}\n\n${prompt}`
37-
37+
let { prompt } = JSON.parse(text);
3838
const relevantDocuments = await retriever.getRelevantDocuments(prompt);
3939

40-
const chain = await getChain((token) => {
40+
const chain = await getRagChain((token) => {
4141
res.write(token);
4242
});
4343

4444
await chain.call({
4545
input_documents: relevantDocuments,
46-
question: templated,
46+
question: `${SYSTEM_PROMPT}\n\n${prompt}`,
4747
});
4848

4949
const sources = new Set(
@@ -62,6 +62,21 @@ app.post("/", async (req, res) => {
6262
res.end();
6363
});
6464

65+
app.post("/v1/models/generic/prompt", async (req, res) => {
66+
const decoder = new TextDecoder();
67+
const text = decoder.decode(req.body);
68+
69+
let { prompt } = JSON.parse(text);
70+
71+
const chain = await getOpenAIChat((token) => {
72+
res.write(token);
73+
});
74+
75+
await chain.call(`${SYSTEM_PROMPT}\n\n${prompt}`);
76+
77+
res.end();
78+
});
79+
6580
app.get("/v1/health", (_, res) => {
6681
res.send("OK");
6782
});

0 commit comments

Comments
 (0)