mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-07-12 17:04:40 -05:00
47 lines
1.1 KiB
TypeScript
47 lines
1.1 KiB
TypeScript
import {
|
|
HttpClient,
|
|
HttpDownloadProgressEvent,
|
|
HttpEventType,
|
|
} from '@angular/common/http'
|
|
import { Injectable } from '@angular/core'
|
|
import { filter, map, Observable } from 'rxjs'
|
|
import { environment } from 'src/environments/environment'
|
|
|
|
export interface ChatMessage {
|
|
role: 'user' | 'assistant'
|
|
content: string
|
|
isStreaming?: boolean
|
|
}
|
|
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
export class ChatService {
|
|
constructor(private http: HttpClient) {}
|
|
|
|
streamChat(documentId: number, prompt: string): Observable<string> {
|
|
return this.http
|
|
.post(
|
|
`${environment.apiBaseUrl}documents/chat/`,
|
|
{
|
|
document_id: documentId,
|
|
q: prompt,
|
|
},
|
|
{
|
|
observe: 'events',
|
|
reportProgress: true,
|
|
responseType: 'text',
|
|
withCredentials: true,
|
|
}
|
|
)
|
|
.pipe(
|
|
map((event) => {
|
|
if (event.type === HttpEventType.DownloadProgress) {
|
|
return (event as HttpDownloadProgressEvent).partialText!
|
|
}
|
|
}),
|
|
filter((chunk) => !!chunk)
|
|
)
|
|
}
|
|
}
|