Skip to content

Commit

Permalink
fix: ok, that was silly! should have booted server using server.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
ydennisy committed Nov 19, 2023
1 parent 8516f26 commit 4981018
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"scripts": {
"dev": "nodemon --watch 'src/**/*.ts' --exec 'ts-node' src/server.ts",
"build": "tsc",
"start": "node dist/app.js",
"start": "node dist/server.js",
"test": "tap run"
},
"devDependencies": {
Expand Down
13 changes: 4 additions & 9 deletions backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import { PrismaNodeRepo } from './repo';
import { parse } from './parser';
import { embed } from './embedder';
import { scrape } from './scraper';
import { generateTitle } from './augmentor';
import OpenAI from 'openai';

const openai = new OpenAI();
import { generateTitle, summariseOrAnswerFromDocuments } from './llm';

interface GetNodeParams {
id: number;
Expand Down Expand Up @@ -122,8 +119,7 @@ app.get<{ Querystring: GetSearchParams }>('/chat', async (req, res) => {
const { q } = req.query;
const queryEmbedding = await embed(q);
const results = await nodeRepo.search(queryEmbedding);
const titles = results.map((node) => `- ${node.toDTO().title}`).join('\n');
const input = `
/* const input = `
Given the following list of documents, and query, you must
answer the question, or summarise the docs depending on the tone.
----
Expand All @@ -133,13 +129,12 @@ app.get<{ Querystring: GetSearchParams }>('/chat', async (req, res) => {
QUERY:
${q}
`;
console.log(input);
const stream = await openai.chat.completions.create({
model: 'gpt-3.5-turbo',
messages: [{ role: 'user', content: input }],
stream: true,
});

}); */
const stream = await summariseOrAnswerFromDocuments(results, q);
res.raw.writeHead(200, { 'Content-Type': 'text/plain' });
for await (const part of stream) {
res.raw.write(part.choices[0]?.delta?.content || '');
Expand Down
24 changes: 22 additions & 2 deletions backend/src/augmentor.ts → backend/src/llm.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import OpenAI from 'openai';

import { Node } from './node';
const openai = new OpenAI();

const generateTitle = async (text: string): Promise<string> => {
Expand Down Expand Up @@ -44,4 +44,24 @@ const generateTitle = async (text: string): Promise<string> => {
return title;
};

export { generateTitle };
const summariseOrAnswerFromDocuments = async (nodes: Node[], query: string) => {
const titles = nodes.map((node) => `- ${node.toDTO().title}`).join('\n');
const input = `
Given the following list of documents, and query, you must
answer the question, or summarise the docs depending on the tone.
----
DOCUMENTS:
${titles}
----
QUERY:
${query}
`;
const stream = await openai.chat.completions.create({
model: 'gpt-3.5-turbo',
messages: [{ role: 'user', content: input }],
stream: true,
});
return stream;
};

export { generateTitle, summariseOrAnswerFromDocuments };
2 changes: 1 addition & 1 deletion backend/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { app } from './app';

const start = async () => {
try {
await app.listen({ port: 8000 });
await app.listen({ port: Number(process.env.PORT) || 8000 });
} catch (err) {
app.log.error(err);
process.exit(1);
Expand Down

0 comments on commit 4981018

Please sign in to comment.