-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Johannes Tammekänd
authored and
Johannes Tammekänd
committed
Jul 25, 2024
1 parent
a32304b
commit 7022bc1
Showing
3 changed files
with
131 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters