Skip to content

Commit

Permalink
Merge pull request #23 from appwrite/feat-v1-prompt
Browse files Browse the repository at this point in the history
feat: v1 prompt
  • Loading branch information
christyjacob4 authored Dec 16, 2024
2 parents 4b277b7 + 92b5233 commit 8cf92ef
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 24 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ node_modules
.vscode
.env
venv
sources
sources
.DS_Store
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ The server exposes a POST endpoint at `/`. The endpoint expects a raw text body
Use cURL to test the server, for example:

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

## Contributing
Expand Down
2 changes: 1 addition & 1 deletion scripts/test-prompts.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { writeFile, mkdir } from "fs/promises";

const SERVER_URL = "http://localhost:3003/";
const SERVER_URL = "http://localhost:3003/v1/models/assistant/prompt";
const PROMPTS = [
"How do you add a custom domain in the console?",
"Show me how I can set up database collections and documents.",
Expand Down
29 changes: 16 additions & 13 deletions src/embeddings.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,22 @@ export const intializeDocumentRetriever = async () => {
return vectorStore.asRetriever(5);
};

export const getChain = async (onToken) => {
export const getOpenAIChat = async (onToken) =>
new OpenAIChat({
modelName: "gpt-4o",
openAIApiKey: process.env._APP_ASSISTANT_OPENAI_API_KEY,
temperature: 0.3,
maxTokens: 1000,
streaming: true,
callbacks: [
{
handleLLMNewToken: onToken,
},
],
});

export const getRagChain = async (onToken) => {
return loadQAStuffChain(
new OpenAIChat({
modelName: "gpt-4o",
openAIApiKey: process.env._APP_ASSISTANT_OPENAI_API_KEY,
temperature: 0.3,
maxTokens: 1000,
streaming: true,
callbacks: [
{
handleLLMNewToken: onToken,
},
],
})
(await getOpenAIChat(onToken)),
);
};
31 changes: 23 additions & 8 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import bodyParser from "body-parser";
import cors from "cors";
import express from "express";
import {
getChain,
getRagChain,
getOpenAIChat,
intializeDocumentRetriever as initializeRetriever,

} from "./embeddings.js";

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

const port = 3003;

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.";
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.";

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

let { prompt, systemPrompt } = JSON.parse(text);
const templated = `${systemPrompt ?? DEFAULT_SYSTEM_PROMPT}\n\n${prompt}`

let { prompt } = JSON.parse(text);
const relevantDocuments = await retriever.getRelevantDocuments(prompt);

const chain = await getChain((token) => {
const chain = await getRagChain((token) => {
res.write(token);
});

await chain.call({
input_documents: relevantDocuments,
question: templated,
question: `${SYSTEM_PROMPT}\n\n${prompt}`,
});

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

app.post("/v1/models/generic/prompt", async (req, res) => {
const decoder = new TextDecoder();
const text = decoder.decode(req.body);

let { prompt } = JSON.parse(text);

const chain = await getOpenAIChat((token) => {
res.write(token);
});

await chain.call(`${SYSTEM_PROMPT}\n\n${prompt}`);

res.end();
});

app.get("/v1/health", (_, res) => {
res.send("OK");
});
Expand Down

0 comments on commit 8cf92ef

Please sign in to comment.