-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: some small organizing of the ai related code
- Loading branch information
1 parent
8d6ae76
commit 15623b5
Showing
4 changed files
with
130 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { RunnableWithMessageHistory } from '@langchain/core/runnables'; | ||
import { ChatPromptTemplate, MessagesPlaceholder } from "@langchain/core/prompts"; | ||
import { ChatMessageHistory } from 'langchain/stores/message/in_memory'; | ||
|
||
let sessions = {}; | ||
let chainWithHistory; | ||
|
||
export function createChain(model) { | ||
//////////////////////////////// | ||
// CREATE CHAIN | ||
const prompt = ChatPromptTemplate.fromMessages([ | ||
[ 'system', | ||
'You are a helpful, respectful and honest assistant named "Parasol Assistant".' + | ||
'You will be given a claim summary, references to provide you with information, and a question. You must answer the question based as much as possible on this claim with the help of the references.' + | ||
'Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.' + | ||
'If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don\'t know the answer to a question, please don\'t share false information.' + | ||
'Don\'t make up policy term limits by yourself' | ||
], | ||
new MessagesPlaceholder('history'), | ||
[ 'human', '{input}' ] | ||
]); | ||
|
||
const chain = prompt.pipe(model); | ||
|
||
chainWithHistory = new RunnableWithMessageHistory({ | ||
runnable: chain, | ||
getMessageHistory: (sessionId) => { | ||
if (sessions[sessionId] === undefined) { | ||
sessions[sessionId] = new ChatMessageHistory(); | ||
} | ||
return sessions[sessionId]; | ||
}, | ||
inputMessagesKey: 'input', | ||
historyMessagesKey: 'history', | ||
}); | ||
|
||
} | ||
|
||
export async function answerQuestion(question, sessionId) { | ||
const result = await chainWithHistory.stream( | ||
{ input: createQuestion(question) }, | ||
{ configurable: { sessionId: sessionId } } | ||
); | ||
|
||
return result; | ||
} | ||
|
||
export function resetSessions(sessionId) { | ||
delete sessions[sessionId]; | ||
} | ||
|
||
function createQuestion(rawQuestion) { | ||
return `Claim ID: ${rawQuestion.claimId} | ||
Claim Inception Date: ${rawQuestion.inceptionDate} | ||
Claim Summary: | ||
${rawQuestion.claim} | ||
Question: ${rawQuestion.query} | ||
` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { getModel } from '../ai/ai.mjs'; | ||
import { createChain, answerQuestion, resetSessions } from '../ai/chatbot.mjs'; | ||
|
||
async function chatbotWSRoute (fastify, options) { | ||
fastify.get('/ws/query', { websocket: true }, (ws, req) => { | ||
const controller = new AbortController(); | ||
|
||
ws.on('close', () => { | ||
resetSessions(ws); | ||
controller.abort(); | ||
console.log('connection closed'); | ||
}); | ||
|
||
ws.on('error', console.error); | ||
|
||
ws.on('message', async (data) => { | ||
const stringData = data.toString(); | ||
|
||
// This should be JSON | ||
let JSONmessage; | ||
try { | ||
JSONmessage = JSON.parse(stringData); | ||
} catch(err) { | ||
console.log(err); | ||
} | ||
|
||
console.log('Query from the Client', JSONmessage); | ||
|
||
console.log('Starting to Ask', new Date()); | ||
|
||
try { | ||
const answerStream = await answerQuestion(JSONmessage, ws); | ||
|
||
for await (const chunk of answerStream) { | ||
console.log(`Got Chat Response: ${chunk.content}`); | ||
|
||
//'{"type":"token","token":" Hello","source":""}' | ||
const formattedAnswer = { | ||
type: 'token', | ||
token: chunk.content, | ||
source: '' | ||
}; | ||
|
||
ws.send(JSON.stringify(formattedAnswer)); | ||
} | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
|
||
console.log('Done Asking', new Date()); | ||
}); | ||
|
||
// AI Related Setup | ||
const model = getModel().bind({ signal: controller.signal }); | ||
createChain(model); | ||
}); | ||
} | ||
|
||
export default chatbotWSRoute; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters