forked from TBXark/ChatGPT-Telegram-Workers
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow quick switching of models even if you don't know the deta…
…iled name. feat: now bring back the audio dialog function and select either transcription or dialog via AUDIO_HANDLE_TYPE perf: optimize the /set command so that even if a shortcut key is not set, you can still modify the KEY environment variable using -KEY. Switch models by querying similarity through //c //v. You can modify the variable RERANK_MODELS to customize the base data. Process the base data through the default JINA RERANK MODEL. The default rerank service provider is jina, with the model being jina-colbert-v2. This processes the base data, currently also supporting obtaining embeddings data through the openai embedding model, and then indirectly obtaining values by finding the maximum cosine similarity value. You need to enable ENABLE_INTELLIGENT_MODEL to use this feature.
- Loading branch information
Showing
11 changed files
with
178 additions
and
75 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
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
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
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,22 @@ | ||
import type { AgentUserConfig } from '../../config/env'; | ||
|
||
export class JinaClassifier { | ||
readonly model = 'jina-embeddings-v3'; | ||
readonly api = 'https://api.jina.ai/v1/classify'; | ||
|
||
readonly request = async (context: AgentUserConfig, data: string[], labels: string[]) => { | ||
const body = { | ||
model: this.model, | ||
input: data, | ||
labels, | ||
}; | ||
return fetch(this.api, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Authorization': `Bearer ${context.JINA_API_KEY}`, | ||
}, | ||
body: JSON.stringify(body), | ||
}).then(res => res.json()).then(res => res.data.map((item: any) => ({ label: item.prediction, value: data[item.index] }))); | ||
}; | ||
} |
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,47 @@ | ||
import type { AgentUserConfig } from '../../config/env'; | ||
import { createOpenAI } from '@ai-sdk/openai'; | ||
import { embedMany } from 'ai'; | ||
import { OpenAIBase } from '../../agent/openai'; | ||
|
||
export class OpenaiEmbedding extends OpenAIBase { | ||
readonly request = async (context: AgentUserConfig, data: string[]) => { | ||
const { embeddings, values } = await embedMany({ | ||
model: createOpenAI({ | ||
baseURL: context.OPENAI_API_BASE, | ||
apiKey: this.apikey(context), | ||
}).embedding(context.OPENAI_EMBEDDING_MODEL), | ||
values: data, | ||
maxRetries: 0, | ||
}); | ||
return values.map((value, i) => ({ embed: embeddings[i], value })); | ||
}; | ||
} | ||
|
||
export class JinaEmbedding { | ||
readonly task: 'retrieval.query' | 'retrieval.passage' | 'separation' | 'classification' | 'text-matching' | undefined; | ||
readonly model = 'jina-embeddings-v3'; | ||
|
||
constructor(task?: 'retrieval.query' | 'retrieval.passage' | 'separation' | 'classification' | 'text-matching') { | ||
this.task = task; | ||
} | ||
|
||
readonly request = async (context: AgentUserConfig, data: string[]) => { | ||
const url = 'https://api.jina.ai/v1/embeddings'; | ||
const body = { | ||
model: this.model, | ||
task: this.task, | ||
dimensions: 1024, | ||
late_chunking: false, | ||
embedding_type: 'float', | ||
input: data, | ||
}; | ||
return fetch(url, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Authorization': `Bearer ${context.JINA_API_KEY}`, | ||
}, | ||
body: JSON.stringify(body), | ||
}).then(res => res.json()).then(res => res.data.map((item: any) => ({ embed: item.embedding, value: data[item.index] }))); | ||
}; | ||
} |
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,47 @@ | ||
import type { AgentUserConfig } from '../../config/env'; | ||
import { cosineSimilarity } from 'ai'; | ||
import { OpenaiEmbedding } from './embedding'; | ||
|
||
export class Rerank { | ||
readonly rank = (context: AgentUserConfig, data: string[], topN: number = 1) => { | ||
switch (context.RERANK_AGENT) { | ||
case 'jina': | ||
return this.jina(context, data, topN); | ||
case 'openai': | ||
return this.openai(context, data, topN); | ||
default: | ||
throw new Error('Invalid RERANK_AGENT'); | ||
} | ||
}; | ||
|
||
readonly jina = async (context: AgentUserConfig, data: string[], topN: number) => { | ||
const url = 'https://api.jina.ai/v1/rerank'; | ||
const result = await fetch(url, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Authorization': `Bearer ${context.JINA_API_KEY}`, | ||
}, | ||
body: JSON.stringify({ | ||
model: context.JINA_RERANK_MODEL, | ||
query: data[0], | ||
documents: data.slice(1), | ||
top_n: topN, | ||
}), | ||
}).then(res => res.json()); | ||
if (!result.results) { | ||
throw new Error(`No results found. details: ${JSON.stringify(result)}`); | ||
} | ||
return result.results.map((item: any) => ({ similar: item.relevance_score, name: item.document.text })); | ||
}; | ||
|
||
readonly openai = async (context: any, data: string[], topN: number) => { | ||
const embeddings = await new OpenaiEmbedding().request(context, data); | ||
const inputEmbeddings = embeddings[0].embed; | ||
return embeddings.slice(1) | ||
.map(({ embed, value }) => ({ similar: cosineSimilarity(inputEmbeddings, embed), value })) | ||
.sort((a, b) => b.similar - a.similar) | ||
.slice(0, topN) | ||
.map(i => i.value); | ||
}; | ||
} |
This file was deleted.
Oops, something went wrong.