Skip to content

Commit 214b8c9

Browse files
committed
add assistant instructions attachement funcionality
1 parent f24712b commit 214b8c9

File tree

4 files changed

+44
-11
lines changed

4 files changed

+44
-11
lines changed

src/client/components/ChatV2/ChatV2.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ export const ChatV2 = () => {
4343

4444
// local storage
4545
const localStoragePrefix = 'general'
46-
const [system, setSystem] = useLocalStorageState<{ content: string }>(`${localStoragePrefix}-chat-system`, { content: '' })
46+
// TODO: Do translation
47+
const defaultInstructions = 'Olet avulias avustaja'
48+
const [assistantInstructions, setAssistantInstructions] = useLocalStorageState<{ content: string }>(`${localStoragePrefix}-chat-instructions`, {
49+
content: defaultInstructions,
50+
})
4751
const [message, setMessage] = useLocalStorageState<{ content: string }>(`${localStoragePrefix}-chat-current`, { content: '' })
4852
const [messages, setMessages] = useLocalStorageState<Message[]>(`${localStoragePrefix}-chat-messages`, [])
4953
const [prevResponse, setPrevResponse] = useLocalStorageState<{ id: string }>(`${localStoragePrefix}-prev-response`, { id: '' })
@@ -166,7 +170,7 @@ export const ChatV2 = () => {
166170

167171
try {
168172
const { tokenUsageAnalysis, stream } = await getCompletionStream({
169-
system: system.content,
173+
assistantInstructions: assistantInstructions.content,
170174
messages: newMessages,
171175
ragIndexId: ragIndexId ?? undefined,
172176
model: model.name,
@@ -408,6 +412,8 @@ export const ChatV2 = () => {
408412
<SettingsModal
409413
open={settingsModalOpen}
410414
setOpen={setSettingsModalOpen}
415+
assistantInstructions={assistantInstructions.content}
416+
setAssistantInstructions={(updatedInstructions) => setAssistantInstructions({ content: updatedInstructions })}
411417
model={model.name}
412418
setModel={(name) => setModel({ name })}
413419
setRagIndex={setRagIndexId}

src/client/components/ChatV2/SettingsModal.tsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,26 @@ import { RagIndexAttributes } from '../../../shared/types'
99
interface SettingsModalProps {
1010
open: boolean
1111
setOpen: (open: boolean) => void
12+
assistantInstructions: string
13+
setAssistantInstructions: (instructions: string) => void
1214
model: string
1315
setModel: (model: string) => void
1416
setRagIndex: (ragIndex: number) => void
1517
ragIndices: RagIndexAttributes[]
1618
currentRagIndex: RagIndexAttributes
1719
}
1820

19-
export const SettingsModal: React.FC<SettingsModalProps> = ({ open, setOpen, model, setModel, setRagIndex, ragIndices, currentRagIndex }) => {
21+
export const SettingsModal: React.FC<SettingsModalProps> = ({
22+
open,
23+
setOpen,
24+
assistantInstructions,
25+
setAssistantInstructions,
26+
model,
27+
setModel,
28+
setRagIndex,
29+
ragIndices,
30+
currentRagIndex,
31+
}) => {
2032
return (
2133
<Modal open={open} onClose={() => setOpen(false)}>
2234
<Box
@@ -51,7 +63,14 @@ export const SettingsModal: React.FC<SettingsModalProps> = ({ open, setOpen, mod
5163
{/* <ModelSelector currentModel={model} setModel={setModel} models={validModels.map((m) => m.name)} /> */}
5264
{/* Disabled for now due to RAG not functioning cirreclty */}
5365
{/* <RagSelector currentRagIndex={currentRagIndex} setRagIndex={setRagIndex} ragIndices={ragIndices} /> */}
54-
<TextField multiline minRows={6} maxRows={10} label="Alustuksen sisältö" defaultValue="Olet avulias avustaja" />
66+
<TextField
67+
multiline
68+
minRows={6}
69+
maxRows={10}
70+
label="Alustuksen sisältö"
71+
value={assistantInstructions}
72+
onChange={(e) => setAssistantInstructions(e.target.value)}
73+
/>
5574
</Box>
5675
</Modal>
5776
)

src/client/components/ChatV2/util.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Message } from '../../types'
33
import { postAbortableStream } from '../../util/apiClient'
44

55
interface GetCompletoinStreamProps {
6-
system: string
6+
assistantInstructions: string
77
messages: Message[]
88
model: string
99
formData: FormData
@@ -16,7 +16,7 @@ interface GetCompletoinStreamProps {
1616
saveConsent: boolean
1717
}
1818
export const getCompletionStream = async ({
19-
system,
19+
assistantInstructions,
2020
messages,
2121
model,
2222
formData,
@@ -34,10 +34,11 @@ export const getCompletionStream = async ({
3434
messages: [
3535
{
3636
role: 'system',
37-
content: system,
37+
content: assistantInstructions,
3838
},
3939
...messages,
4040
],
41+
assistantInstructions,
4142
ragIndexId,
4243
model,
4344
userConsent,

src/server/routes/openai.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const fileParsing = async (options: any, req: any) => {
5050
const PostStreamSchemaV2 = z.object({
5151
options: z.object({
5252
model: z.string(),
53+
assistantInstructions: z.string().optional(),
5354
messages: z.array(z.any()),
5455
userConsent: z.boolean().optional(),
5556
modelTemperature: z.number().optional(),
@@ -153,19 +154,22 @@ openaiRouter.post('/stream/v2', upload.single('file'), async (r, res) => {
153154

154155
// Check rag index
155156
let vectorStoreId: string | undefined = undefined
156-
let instructions: string | undefined = undefined
157+
let instructions: string | undefined = options.assistantInstructions
157158

158159
if (ragIndexId && user.isAdmin) {
159160
const ragIndex = await RagIndex.findByPk(ragIndexId)
160161
if (ragIndex) {
161162
if (courseId && ragIndex.courseId !== courseId) {
162-
logger.error('RagIndex does not belong to the course', { ragIndexId, courseId })
163+
logger.error('RagIndex does not belong to the course', {
164+
ragIndexId,
165+
courseId,
166+
})
163167
res.status(403).send('RagIndex does not belong to the course')
164168
return
165169
}
166170

167171
vectorStoreId = ragIndex.metadata.azureVectorStoreId
168-
instructions = ragIndex.metadata.instructions ?? DEFAULT_RAG_SYSTEM_PROMPT
172+
instructions = `${instructions} ${ragIndex.metadata.instructions ?? DEFAULT_RAG_SYSTEM_PROMPT}`
169173

170174
console.log('using', ragIndex.toJSON())
171175
} else {
@@ -407,7 +411,10 @@ openaiRouter.post('/stream/:courseId/:version?', upload.single('file'), async (r
407411
let events
408412
if (version === 'v2') {
409413
const latestMessage = options.messages[options.messages.length - 1] // Adhoc to input only the latest message
410-
events = await responsesClient.createResponse({ input: [latestMessage], prevResponseId: options.prevResponseId })
414+
events = await responsesClient.createResponse({
415+
input: [latestMessage],
416+
prevResponseId: options.prevResponseId,
417+
})
411418
} else {
412419
events = await getCompletionEvents(options)
413420
}

0 commit comments

Comments
 (0)