Skip to content

Commit

Permalink
399th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Nov 15, 2024
1 parent 0bc44c8 commit ab6f0b1
Showing 1 changed file with 31 additions and 30 deletions.
61 changes: 31 additions & 30 deletions app/src/routes/sse/model/+handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,43 @@ import type { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox';
import type { Document } from '@langchain/core/documents';
import { ChatPromptTemplate } from '@langchain/core/prompts';
import { MongoDBAtlasVectorSearch } from '@langchain/mongodb';
import { Type } from '@sinclair/typebox';
import { createStuffDocumentsChain } from 'langchain/chains/combine_documents';
import { createRetrievalChain } from 'langchain/chains/retrieval';

import useModel, { useEmbeddings } from '~/composables/useModel';

export default (async (app) => {
app.post(
'',
{
schema: {
body: Type.Object({
message: Type.String(),
}),
},
},
async (request, reply) => {
const model = useModel({
// Replace the model with a fine-tuned model
model: 'gpt-4o-mini',

// Allow some creative flexibility to handle variations in phrasing, while maintaining accuracy in responses
temperature: 0.3,
});

const stream = await model.stream(request.body.message);

for await (const chunk of stream) {
reply.sse({ data: chunk.content });
}

request.raw.on('close', async () => {
await stream.cancel();
});
},
);
/**
* ```ts
* import { stream } from 'fetch-event-stream';
*
* await stream('http://127.0.0.1:3000/api/sse/model', {
* method: 'POST',
* body: JSON.stringify({ message: 'What is GenAI?' }),
* });
* ```
*/
app.post('', async (request, reply) => {
const body = JSON.parse(request.body as string) as { message: string };

const model = useModel({
// Replace the model with a fine-tuned model
model: 'gpt-4o-mini',

// Allow some creative flexibility to handle variations in phrasing, while maintaining accuracy in responses
temperature: 0.3,
});

const stream = await model.stream(body.message);

for await (const chunk of stream) {
reply.sse({ data: chunk.content });
}

request.raw.on('close', async () => {
await stream.cancel();
});
});

app.get('/docs', async (request, reply) => {
const collection = app.mongo.db?.collection('vector') as mongodb.Collection<mongodb.Document>;
Expand Down

0 comments on commit ab6f0b1

Please sign in to comment.