Skip to content

Commit

Permalink
Add groq example with llama
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes Tammekänd authored and Johannes Tammekänd committed Jul 25, 2024
1 parent a32304b commit 7022bc1
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 2 deletions.
127 changes: 127 additions & 0 deletions examples/chat_groq.ts
Original file line number Diff line number Diff line change
@@ -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<string | undefined> {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})

const question = (query: string): Promise<string> => {
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<Message[]> {
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"))
3 changes: 2 additions & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion examples/template.env
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ PRIVATE_KEY=""
CHAT_CONTRACT_ADDRESS="0x4A5e76a1aEa072BF32a71A61F52FC1f410AAd748"
CHAT_VISION_CONTRACT_ADDRESS="0x785578B0dA5F21F8321590981E15F618BBc1915c"
AGENT_CONTRACT_ADDRESS="0xFb09a7a940ae690Fafc59e18310c4deBF75B1B52"
ANTROPIC_CONTRACT_ADDRESS="0x8cA1e115f96A562418968B475c1F096a8A385Ddb"
ANTROPIC_CONTRACT_ADDRESS="0x8cA1e115f96A562418968B475c1F096a8A385Ddb"
GROQ_CONTRACT_ADDRESS="0xD11E905888bF89DDf35E7E3F8a9da923E9694225"

0 comments on commit 7022bc1

Please sign in to comment.