Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix: handle API request errors and log them in llamacpp extension (#7094)

Akarshan Biswasf471a66a55e3
diff --git a/extensions/llamacpp-extension/src/index.ts b/extensions/llamacpp-extension/src/index.ts
index d598edfb2..d7e8e7890 100644
--- a/extensions/llamacpp-extension/src/index.ts
+++ b/extensions/llamacpp-extension/src/index.ts
@@ -1160,7 +1160,10 @@ export default class llamacpp_extension extends AIEngine {
for (const modelId of modelIds) {
const path = await joinPath([modelsDir, modelId, 'model.yml'])
const modelConfig = await invoke<ModelConfig>('read_yaml', { path })
- const isEmbedding = await this.resolveEmbeddingConfig(modelId, modelConfig)
+ const isEmbedding = await this.resolveEmbeddingConfig(
+ modelId,
+ modelConfig
+ )
const modelInfo = {
id: modelId,
@@ -2342,7 +2345,10 @@ export default class llamacpp_extension extends AIEngine {
: 512) || 512
const batches = buildEmbedBatches(text, ubatchSize)
- const attemptRequest = async (session: SessionInfo, batchInput: string[]) => {
+ const attemptRequest = async (
+ session: SessionInfo,
+ batchInput: string[]
+ ) => {
const baseUrl = `http://localhost:${session.port}/v1/embeddings`
const headers = {
'Content-Type': 'application/json',
@@ -2631,44 +2637,49 @@ export default class llamacpp_extension extends AIEngine {
},
}
- let parseResponse = await fetch(`${baseUrl}/apply-template`, {
- method: 'POST',
- headers: headers,
- body: JSON.stringify(tokenizeRequest),
- })
+ try {
+ let parseResponse = await fetch(`${baseUrl}/apply-template`, {
+ method: 'POST',
+ headers: headers,
+ body: JSON.stringify(tokenizeRequest),
+ })
- if (!parseResponse.ok) {
- const errorData = await parseResponse.json().catch(() => null)
- throw new Error(
- `API request failed with status ${
- parseResponse.status
- }: ${JSON.stringify(errorData)}`
- )
- }
+ if (!parseResponse.ok) {
+ const errorData = await parseResponse.json().catch(() => null)
+ throw new Error(
+ `API request failed with status ${
+ parseResponse.status
+ }: ${JSON.stringify(errorData)}`
+ )
+ }
- const parsedPrompt = await parseResponse.json()
+ const parsedPrompt = await parseResponse.json()
- const response = await fetch(`${baseUrl}/tokenize`, {
- method: 'POST',
- headers: headers,
- body: JSON.stringify({
- content: parsedPrompt.prompt,
- }),
- })
+ const response = await fetch(`${baseUrl}/tokenize`, {
+ method: 'POST',
+ headers: headers,
+ body: JSON.stringify({
+ content: parsedPrompt.prompt,
+ }),
+ })
- if (!response.ok) {
- const errorData = await response.json().catch(() => null)
- throw new Error(
- `API request failed with status ${response.status}: ${JSON.stringify(
- errorData
- )}`
- )
- }
+ if (!response.ok) {
+ const errorData = await response.json().catch(() => null)
+ throw new Error(
+ `API request failed with status ${response.status}: ${JSON.stringify(
+ errorData
+ )}`
+ )
+ }
- const dataTokens = await response.json()
- const textTokens = dataTokens.tokens?.length || 0
+ const dataTokens = await response.json()
+ const textTokens = dataTokens.tokens?.length || 0
- return textTokens + imageTokens
+ return textTokens + imageTokens
+ } catch (e) {
+ console.warn(String(e))
+ }
+ return 0
}
private async calculateImageTokens(