Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | 1x 23x 23x 12x 11x 35x 23x 1x 1x 1x 1x 1x 1x 28x 28x 28x 1x 1x 1x 4x 4x 2x 2x 2x 1x 4x 4x 4x 3x 1x 1x 4x 4x 3x 2x 2x 2x 2x 1x 2x 2x 1x 2x 2x 1x 5x 5x 5x 2x 1x | import axios from 'axios';
import { requestLogger, responseLogger } from 'axios-logger';
import * as FormData from 'form-data';
// import { fetchEventSource } from '@microsoft/fetch-event-source';
export interface IApiClientOptions {
debug: boolean;
url?: string;
timeout?: number;
}
const API_URL = 'https://gigachat.devices.sberbank.ru';
export class ApiClient{
axios: any;
token: any;
constructor(jwt: string, options: IApiClientOptions) {
this.token = jwt;
this.axios = axios.create({
baseURL: options.url || API_URL,
headers: {
'Accept': 'application/json',
Authorization: `Bearer ${this.token}`,
'Content-Type': 'application/json'
},
timeout: options.timeout || 20 * 1000,
});
if (options.debug) {
const config = {
prefixText: 'GigachatApiClient',
status: true,
headers: false,
params: true,
};
this.axios.interceptors.request.use((request: any) => {
return requestLogger(request, config);
});
this.axios.interceptors.response.use((response: any) => {
return responseLogger(response, config);
});
}
}
/* Files */
async getFileList(){
const response = await this.axios.get(`/api/v1/files`);
return response.data;
}
async getFileInfo(fileId: string){
const response = await this.axios.get(`/api/v1/files/${fileId}`);
return response.data;
}
async uploadFile(file: File | Buffer | any, filename?: string){
const formData = new FormData();
// В Node.js используем Buffer или Stream, в браузере - File
if (file instanceof Buffer) {
formData.append('file', file, filename || 'file.txt');
} else if (file && typeof file === 'object' && 'name' in file) {
// File объект (браузер или Node.js 18+)
formData.append('file', file, file.name || filename || 'file.txt');
} else {
formData.append('file', file, filename || 'file.txt');
}E
formData.append('purpose','general');
const response = await this.axios.post(`/api/v1/files`, formData, {
headers: {
...formData.getHeaders(),
},
});
return response.data;
}
async downloadFile(fileId: string, filename: string, created_at: number){
return await this.axios.get(`/api/v1/files/${fileId}/content`, {
responseType: 'blob',
})
/*
.then(response => {
const file = new File([response], filename, {lastModified: created_at})
const a = document.createElement('a')
const url = URL.createObjectURL(file)
a.href = url
a.download = filename
document.body.appendChild(a)
a.click()
setTimeout(() => {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 10)
})
*/
}
async deleteFile(fileId: string){
const response = await this.axios.post(`/api/v1/files/${fileId}/delete`);
return response.data;
}
/* Tokens */
async getAvailableTokens(){
const response = await this.axios.get(`/api/v1/balance`);
return response.data;
}
/* Models */
async getModels(){
const response = await this.axios.get(`/api/v1/models`)
return response.data;
}
/* Requests */
async sendRequest(model: string, messages: object[]){
const data = JSON.stringify({
'model' : model,
'messages' : messages,
}, null,' ')
const response = await this.axios.post(`/api/v1/chat/completions`, data)
return response.data;
}
/*
async sendStreamRequest(model: string, messages: object[], stream: boolean, update_interval: number|null = null){
const data = JSON.stringify({
'model' : model,
'messages' : messages,
'stream' : stream,
'update_interval': update_interval,
}, null,' ')
const pre = '**[ApiClient][Request]** '
// const url = ' ' + `${import.meta.env.VITE_LOG_API_URL}` + '/chat/completions' + ' '
// this.logStore.appendLog(pre + 'post' + url + data)
const logStore = this.logStore
const logUrl = this.logUrl
const response = <string[]>[]
await fetchEventSource(this.url + '/chat/completions',{
method: "POST",
headers: {
'Accept': 'text/event-stream',
Authorization: `Bearer ${this.token}`,
},
body: data,
//@ts-ignore
onopen(response) {
const pre = '**[ApiClient][Response]** '
const url = ' ' + logUrl + '/chat/completions' + ' '
logStore.appendLog(pre + 'post' + url + ' ' + response.status + ': ' + response.statusText)
},
async onmessage(ev) {
const pre = '**[ApiClient][SSE]** '
if (ev.data != '[DONE]'){
const data = JSON.parse(ev.data)
const m = data.choices[0].delta.content as string
logStore.appendLog(pre + ' ' + ev.data)
response.push(m)
}
else if (ev.data == '[DONE]'){
logStore.appendLog(pre + ' ' + ev.data)
}
},
onerror(err) {
console.log("There was an error from server", err);
return
},
})
return response
}
*/
}
|