From 7022bc10481245fe510bfab5a22e105d34ec67b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Tammek=C3=A4nd?= Date: Thu, 25 Jul 2024 17:40:13 +0300 Subject: [PATCH] Add groq example with llama --- examples/chat_groq.ts | 127 ++++++++++++++++++++++++++++++++++++++++++ examples/package.json | 3 +- examples/template.env | 3 +- 3 files changed, 131 insertions(+), 2 deletions(-) create mode 100644 examples/chat_groq.ts diff --git a/examples/chat_groq.ts b/examples/chat_groq.ts new file mode 100644 index 0000000..5598ca5 --- /dev/null +++ b/examples/chat_groq.ts @@ -0,0 +1,127 @@ +import { Contract, ethers, TransactionReceipt, Wallet } from "ethers"; +import ABI from "./abis/ChatGpt.json"; +import * as readline from 'readline'; + +require("dotenv").config() + +interface Message { + role: string, + content: string, +} + +async function main() { + const rpcUrl = process.env.RPC_URL + if (!rpcUrl) throw Error("Missing RPC_URL in .env") + const privateKey = process.env.PRIVATE_KEY + if (!privateKey) throw Error("Missing PRIVATE_KEY in .env") + const contractAddress = process.env.GROQ_CONTRACT_ADDRESS + if (!contractAddress) throw Error("Missing GROQ_CONTRACT_ADDRESS in .env") + + const provider = new ethers.JsonRpcProvider(rpcUrl) + const wallet = new Wallet( + privateKey, provider + ) + const contract = new Contract(contractAddress, ABI, wallet) + + // The message you want to start the chat with + const message = await getUserInput() + + // Call the startChat function + const transactionResponse = await contract.startChat(message) + const receipt = await transactionResponse.wait() + console.log(`Message sent, tx hash: ${receipt.hash}`) + console.log(`Chat started with message: "${message}"`) + + // Get the chat ID from transaction receipt logs + let chatId = getChatId(receipt, contract); + console.log(`Created chat ID: ${chatId}`) + if (!chatId && chatId !== 0) { + return + } + + let allMessages: Message[] = [] + // Run the chat loop: read messages and send messages + while (true) { + const newMessages: Message[] = await getNewMessages(contract, chatId, allMessages.length); + if (newMessages) { + for (let message of newMessages) { + console.log(`${message.role}: ${message.content}`) + allMessages.push(message) + if (allMessages.at(-1)?.role == "assistant") { + const message = getUserInput() + const transactionResponse = await contract.addMessage(message, chatId) + const receipt = await transactionResponse.wait() + console.log(`Message sent, tx hash: ${receipt.hash}`) + } + } + } + await new Promise(resolve => setTimeout(resolve, 2000)) + } + +} + +async function getUserInput(): Promise { + const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout + }) + + const question = (query: string): Promise => { + return new Promise((resolve) => { + rl.question(query, (answer) => { + resolve(answer) + }) + }) + } + + try { + const input = await question("Message Groq: ") + rl.close() + return input + } catch (err) { + console.error('Error getting user input:', err) + rl.close() + } +} + + +function getChatId(receipt: TransactionReceipt, contract: Contract) { + let chatId + for (const log of receipt.logs) { + try { + const parsedLog = contract.interface.parseLog(log) + if (parsedLog && parsedLog.name === "ChatCreated") { + // Second event argument + chatId = ethers.toNumber(parsedLog.args[1]) + } + } catch (error) { + // This log might not have been from your contract, or it might be an anonymous log + console.log("Could not parse log:", log) + } + } + return chatId; +} + +async function getNewMessages( + contract: Contract, + chatId: number, + currentMessagesCount: number +): Promise { + const messages = await contract.getMessageHistory(chatId) + + const newMessages: Message[] = [] + messages.forEach((message: any, i: number) => { + if (i >= currentMessagesCount) { + newMessages.push( + { + role: message[0], + content: message.content[0].value, + } + ); + } + }) + return newMessages; +} + +main() + .then(() => console.log("Done")) \ No newline at end of file diff --git a/examples/package.json b/examples/package.json index 8bc6179..dd860dd 100644 --- a/examples/package.json +++ b/examples/package.json @@ -7,7 +7,8 @@ "simpleChat": "ts-node simpleLlmChat.ts", "chat_vision": "ts-node chat_vision.ts", "chat_anthropic": "ts-node chat_anthropic.ts", - "agent": "ts-node agent.ts" + "agent": "ts-node agent.ts", + "chatGroq": "ts-node chat_groq.ts" }, "author": "", "license": "ISC", diff --git a/examples/template.env b/examples/template.env index e373294..b825512 100644 --- a/examples/template.env +++ b/examples/template.env @@ -5,4 +5,5 @@ PRIVATE_KEY="" CHAT_CONTRACT_ADDRESS="0x4A5e76a1aEa072BF32a71A61F52FC1f410AAd748" CHAT_VISION_CONTRACT_ADDRESS="0x785578B0dA5F21F8321590981E15F618BBc1915c" AGENT_CONTRACT_ADDRESS="0xFb09a7a940ae690Fafc59e18310c4deBF75B1B52" -ANTROPIC_CONTRACT_ADDRESS="0x8cA1e115f96A562418968B475c1F096a8A385Ddb" \ No newline at end of file +ANTROPIC_CONTRACT_ADDRESS="0x8cA1e115f96A562418968B475c1F096a8A385Ddb" +GROQ_CONTRACT_ADDRESS="0xD11E905888bF89DDf35E7E3F8a9da923E9694225"