Skip to content

Commit b7c39da

Browse files
committed
add llms/completion call and stream normalization for vertex and claude models
1 parent e0d9657 commit b7c39da

File tree

2 files changed

+13
-22
lines changed

2 files changed

+13
-22
lines changed

src/modules/llms/api/athropic.ts

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ export const anthropicStreamCompletion = async (
7070
stream: true,
7171
system: config.openAi.chatGpt.chatCompletionContext,
7272
max_tokens: limitTokens ? +config.openAi.chatGpt.maxTokens : undefined,
73-
messages: conversation.filter(c => c.model === model).map(m => { return { content: m.content, role: m.role } })
73+
messages: conversation.filter(c => c.model === model) // .map(m => { return { content: m.content, role: m.role } })
7474
}
7575
let wordCount = 0
7676
let wordCountMinimum = 2
77-
const url = `${API_ENDPOINT}/anthropic/completions`
77+
const url = `${API_ENDPOINT}/llms/completions` // `${API_ENDPOINT}/anthropic/completions`
7878
if (!ctx.chat?.id) {
7979
throw new Error('Context chat id should not be empty after openAI streaming')
8080
}
@@ -90,26 +90,17 @@ export const anthropicStreamCompletion = async (
9090
const msg = chunk.toString()
9191
if (msg) {
9292
if (msg.includes('Input Token:')) {
93-
const regex = /Input Token: (\d+)(.*)/
94-
// Execute the regular expression
95-
const match = regex.exec(msg)
96-
if (match) {
97-
inputTokens = match[1].trim() // Extract the integer part
98-
if (match.length >= 3) {
99-
completion += match[2]
100-
}
101-
}
102-
} else if (msg.startsWith('Output Tokens')) {
103-
outputTokens = msg.split('Output Tokens: ')[1].trim()
93+
const tokenMsg = msg.split('Input Token: ')[1]
94+
inputTokens = tokenMsg.split('Output Tokens: ')[0]
95+
outputTokens = tokenMsg.split('Output Tokens: ')[1]
96+
completion = completion.split('Input Token: ')[0]
97+
} else if (msg.includes('Output Tokens: ')) {
98+
outputTokens = msg.split('Output Tokens: ')[1]
99+
completion = completion.split('Output Tokens: ')[0]
104100
} else {
105101
wordCount++
106102
completion += msg
107-
if (msg.includes('Output Tokens:')) {
108-
outputTokens = msg.split('Output Tokens: ')[1].trim()
109-
// outputTokens = tokenMsg.split('Output Tokens: ')[1].trim()
110-
completion = completion.split('Output Tokens: ')[0]
111-
}
112-
if (wordCount > wordCountMinimum) { // if (chunck === '.' && wordCount > wordCountMinimum) {
103+
if (wordCount > wordCountMinimum) {
113104
if (wordCountMinimum < 64) {
114105
wordCountMinimum *= 2
115106
}
@@ -125,7 +116,7 @@ export const anthropicStreamCompletion = async (
125116
if (e.error_code !== 400) {
126117
throw e
127118
} else {
128-
logger.error(e)
119+
logger.error(e.message)
129120
}
130121
} else {
131122
throw e

src/modules/llms/api/vertex.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ export const vertexStreamCompletion = async (
7272
system: config.openAi.chatGpt.chatCompletionContext,
7373
max_tokens: limitTokens ? +config.openAi.chatGpt.maxTokens : undefined,
7474
messages: conversation.filter(c => c.model === model)
75-
.map(m => { return { parts: { text: m.content }, role: m.role !== 'user' ? 'model' : 'user' } })
75+
// .map(m => { return { parts: { text: m.content }, role: m.role !== 'user' ? 'model' : 'user' } })
7676
}
77-
const url = `${API_ENDPOINT}/vertex/completions/gemini`
77+
const url = `${API_ENDPOINT}/llms/completions` // `${API_ENDPOINT}/vertex/completions/gemini`
7878
if (!ctx.chat?.id) {
7979
throw new Error('Context chat id should not be empty after openAI streaming')
8080
}

0 commit comments

Comments
 (0)