Skip to content

Commit

Permalink
Add AI cost to llm chat history
Browse files Browse the repository at this point in the history
  • Loading branch information
mcjustin committed Nov 14, 2024
1 parent 4160e78 commit 47934b2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
6 changes: 6 additions & 0 deletions static/ips/assets/js/llmChat.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ function insertMessageIntoUi(role, userMessage) {
const responseCell = document.createElement('td');
const promptTokensCell = document.createElement('td');
const completionTokensCell = document.createElement('td');
const costCell = document.createElement('td');

// Append cells to the row
row.appendChild(requestCell);
row.appendChild(responseCell);
row.appendChild(promptTokensCell);
row.appendChild(completionTokensCell);
row.appendChild(costCell);

// Append the row to the chat messages table
chatMessages.appendChild(row);
Expand Down Expand Up @@ -89,11 +91,15 @@ async function sendMessage() {

const promptTokens = data.prompt_tokens;
const completionTokens = data.completion_tokens;
const costInput = parseInt(promptTokens) * 0.15 / 1000000;
const costOutput = parseInt(completionTokens) * 0.6 / 1000000;
const cost = costInput + costOutput;

// Update the existing row with the response and token counts
row.cells[1].textContent = data.content; // Response
row.cells[2].textContent = promptTokens; // Prompt Tokens
row.cells[3].textContent = completionTokens; // Completion Tokens
row.cells[4].textContent = costInput + " (in) + " costOutput + " (out) = " + cost;
} catch (error) {
console.error('Error sending message to LLM:', error);
row.cells[1].textContent = 'Failed to get a response. Please try again.'; // Update response cell with error message
Expand Down
3 changes: 2 additions & 1 deletion static/ips/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@
<th>Your request</th>
<th>LLM Chat Response</th>
<th>Prompt Tokens</th>
<th>Completion Tokens</th>
<th>Response Tokens</th>
<th>Cost in US$ (prompt + response = total)</th>
</tr>
</thead>
<tbody>
Expand Down

0 comments on commit 47934b2

Please sign in to comment.