Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ export type LlmProcessOutput = {
model: string
}
response: Strategy[] | null
inputTokens: number
outputTokens: number
error?: string | null
}

Expand Down
6 changes: 6 additions & 0 deletions lib/utils/llm/gemini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export async function callGemini(llmInput: LlmProcessProps): Promise<LlmProcessO
let output = null
let error = null
const model = llmInput.model || GEMINI_MODELS.gemini20flashExp
let inputTokens: number | undefined
let outputTokens: number | undefined

try {
const aiModel = genAI.getGenerativeModel({
Expand All @@ -28,6 +30,8 @@ export async function callGemini(llmInput: LlmProcessProps): Promise<LlmProcessO
// console.log(JSON.stringify(result))

const content = result.response.text()
inputTokens = result.response.usageMetadata?.promptTokenCount || 0
outputTokens = result.response.usageMetadata?.candidatesTokenCount || 0

try {
output = JSON.parse(content || '[]') as Strategy[]
Expand All @@ -46,6 +50,8 @@ export async function callGemini(llmInput: LlmProcessProps): Promise<LlmProcessO
model
},
response: output,
inputTokens: inputTokens || 0,
outputTokens: outputTokens || 0,
error
}
}
6 changes: 6 additions & 0 deletions lib/utils/llm/grok.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export async function callGrok(llmInput: LlmProcessProps): Promise<LlmProcessOut
let output = null
let error = null
const model = llmInput.model || XAI_MODELS.grok3latest
let inputTokens: number | undefined
let outputTokens: number | undefined

try {
const completion = await apiClient.chat.completions.create({
Expand All @@ -36,6 +38,8 @@ export async function callGrok(llmInput: LlmProcessProps): Promise<LlmProcessOut
})

const outputContent = completion.choices[0].message.content || '{}'
inputTokens = completion.usage?.prompt_tokens || 0
outputTokens = completion.usage?.completion_tokens || 0

try {
const parsed = JSON.parse(outputContent) as { strategies: Strategy[] }
Expand All @@ -55,6 +59,8 @@ export async function callGrok(llmInput: LlmProcessProps): Promise<LlmProcessOut
model
},
response: output,
inputTokens: inputTokens || 0,
outputTokens: outputTokens || 0,
error
}
}
4 changes: 3 additions & 1 deletion lib/utils/llm/mockedAI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export async function llmMockProcess({ prompt }: LlmProcessProps): Promise<LlmPr
name: 'L2 Yield Farming',
risk: StrategyRisk.HIGH
}
]
],
inputTokens: 0,
outputTokens: 0
}
}
4 changes: 3 additions & 1 deletion lib/utils/portfolio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ export const processAddress = async (
provider: 'local',
model: 'local'
},
response: EMPTY_PORTFOLIO_STRATEGIES
response: EMPTY_PORTFOLIO_STRATEGIES,
inputTokens: 0,
outputTokens: 0
}
]
}
Expand Down
4 changes: 3 additions & 1 deletion test/unit/portfolio.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ const mockedLlmOutput: LlmProcessOutput = {
name: 'Example USDC strategy name',
risk: StrategyRisk.LOW
}
]
],
inputTokens: 0,
outputTokens: 0
}

jest.mock('ambire-common/dist/src/consts/networks', () => {
Expand Down