From c3df06d39240cb696f500d544ea767d56cd2fec3 Mon Sep 17 00:00:00 2001 From: Karl Kallas Date: Thu, 27 Jun 2024 16:08:11 +0300 Subject: [PATCH 1/3] simple llm example contracts --- contracts/contracts/AnthropicSimpleLLM.sol | 73 ++++++++++++++++++++++ contracts/contracts/GroqSimpleLLM.sol | 71 +++++++++++++++++++++ 2 files changed, 144 insertions(+) create mode 100644 contracts/contracts/AnthropicSimpleLLM.sol create mode 100644 contracts/contracts/GroqSimpleLLM.sol diff --git a/contracts/contracts/AnthropicSimpleLLM.sol b/contracts/contracts/AnthropicSimpleLLM.sol new file mode 100644 index 0000000..833afc1 --- /dev/null +++ b/contracts/contracts/AnthropicSimpleLLM.sol @@ -0,0 +1,73 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.13; + +import "https://github.com/galadriel-ai/contracts/blob/main/contracts/contracts/interfaces/IOracle.sol"; + +contract SimpleLLM { + address private oracleAddress = ; // https://docs.galadriel.com/oracle-address + uint private runId = 0; + string public message; + string public response; + IOracle.LlmRequest private config; + + constructor() { + config = IOracle.LlmRequest({ + model : "claude-3-5-sonnet-20240620", // "claude-3-5-sonnet-20240620", "claude-3-opus-20240229", "claude-3-sonnet-20240229", "claude-3-haiku-20240307", "claude-2.1", "claude-2.0", "claude-instant-1.2" + frequencyPenalty : 21, // > 20 for null + logitBias : "", // empty str for null + maxTokens : 1000, // 0 for null + presencePenalty : 21, // > 20 for null + responseFormat : "{\"type\":\"text\"}", + seed : 0, // null + stop : "", // null + temperature : 10, // Example temperature (scaled up, 10 means 1.0), > 20 means null + topP : 101, // Percentage 0-100, > 100 means null + tools : "", + toolChoice : "auto", // "none" or "auto" + user : "" // null + }); + } + + function sendMessage(string memory _message) public { + IOracle.Message memory newMessage = IOracle.Message({ + role: "user", + content: new IOracle.Content[](1) + }); + newMessage.content[0] = IOracle.Content({ + contentType: "text", + value: _message + }); + message = _message; + IOracle(oracleAddress).createLlmCall(runId, config); + } + + // required for Oracle + function onOracleLlmResponse( + uint /*_runId*/, + IOracle.LlmResponse memory _response, + string memory _errorMessage + ) public { + require(msg.sender == oracleAddress, "Caller is not oracle"); + if (keccak256(abi.encodePacked(_errorMessage)) != keccak256(abi.encodePacked(""))) { + response = _errorMessage; + } else { + response = _response.content; + } + } + + // required for Oracle + function getMessageHistory(uint /*_runId*/) public view returns (IOracle.Message[] memory) { + IOracle.Message memory newMessage = IOracle.Message({ + role: "user", + content: new IOracle.Content[](1) + }); + newMessage.content[0] = IOracle.Content({ + contentType: "text", + value: message + }); + IOracle.Message[] memory newMessages = new IOracle.Message[](1); + newMessages[0] = newMessage; + return newMessages; + } +} + diff --git a/contracts/contracts/GroqSimpleLLM.sol b/contracts/contracts/GroqSimpleLLM.sol new file mode 100644 index 0000000..aaa0a95 --- /dev/null +++ b/contracts/contracts/GroqSimpleLLM.sol @@ -0,0 +1,71 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.13; + +import "https://github.com/galadriel-ai/contracts/blob/main/contracts/contracts/interfaces/IOracle.sol"; + +contract SimpleLLM { + address private oracleAddress = ; // https://docs.galadriel.com/oracle-address + uint private runId = 0; + string public message; + string public response; + IOracle.GroqRequest private config; + + constructor() { + config = IOracle.GroqRequest({ + model : "mixtral-8x7b-32768", // "llama3-8b-8192", "llama3-70b-8192", "mixtral-8x7b-32768" or "gemma-7b-it" + frequencyPenalty : 21, // > 20 for null + logitBias : "", // empty str for null + maxTokens : 1000, // 0 for null + presencePenalty : 21, // > 20 for null + responseFormat : "{\"type\":\"text\"}", + seed : 0, // null + stop : "", // null + temperature : 10, // Example temperature (scaled up, 10 means 1.0), > 20 means null + topP : 101, // Percentage 0-100, > 100 means null + user : "" // null + }); + } + + function sendMessage(string memory _message) public { + IOracle.Message memory newMessage = IOracle.Message({ + role: "user", + content: new IOracle.Content[](1) + }); + newMessage.content[0] = IOracle.Content({ + contentType: "text", + value: _message + }); + message = _message; + IOracle(oracleAddress).createGroqLlmCall(runId, config); + } + + // required for Oracle + function onOracleGroqLlmResponse( + uint /*_runId*/, + IOracle.GroqResponse memory _response, + string memory _errorMessage + ) public { + require(msg.sender == oracleAddress, "Caller is not oracle"); + if (keccak256(abi.encodePacked(_errorMessage)) != keccak256(abi.encodePacked(""))) { + response = _errorMessage; + } else { + response = _response.content; + } + } + + // required for Oracle + function getMessageHistory(uint /*_runId*/) public view returns (IOracle.Message[] memory) { + IOracle.Message memory newMessage = IOracle.Message({ + role: "user", + content: new IOracle.Content[](1) + }); + newMessage.content[0] = IOracle.Content({ + contentType: "text", + value: message + }); + IOracle.Message[] memory newMessages = new IOracle.Message[](1); + newMessages[0] = newMessage; + return newMessages; + } +} + From e2c5cbf46fe64202c330994aff301635c15dac2e Mon Sep 17 00:00:00 2001 From: Karl Kallas Date: Thu, 27 Jun 2024 16:20:28 +0300 Subject: [PATCH 2/3] fixes import error --- contracts/contracts/AnthropicSimpleLLM.sol | 5 +++-- contracts/contracts/GroqSimpleLLM.sol | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/contracts/contracts/AnthropicSimpleLLM.sol b/contracts/contracts/AnthropicSimpleLLM.sol index 833afc1..92eb7e5 100644 --- a/contracts/contracts/AnthropicSimpleLLM.sol +++ b/contracts/contracts/AnthropicSimpleLLM.sol @@ -1,10 +1,11 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.8.13; -import "https://github.com/galadriel-ai/contracts/blob/main/contracts/contracts/interfaces/IOracle.sol"; +// import "https://github.com/galadriel-ai/contracts/blob/main/contracts/contracts/interfaces/IOracle.sol"; +import "./interfaces/IOracle.sol"; contract SimpleLLM { - address private oracleAddress = ; // https://docs.galadriel.com/oracle-address + address private oracleAddress = 0x68EC9556830AD097D661Df2557FBCeC166a0A075; // use latest: https://docs.galadriel.com/oracle-address uint private runId = 0; string public message; string public response; diff --git a/contracts/contracts/GroqSimpleLLM.sol b/contracts/contracts/GroqSimpleLLM.sol index aaa0a95..d5f19ca 100644 --- a/contracts/contracts/GroqSimpleLLM.sol +++ b/contracts/contracts/GroqSimpleLLM.sol @@ -1,10 +1,11 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.8.13; -import "https://github.com/galadriel-ai/contracts/blob/main/contracts/contracts/interfaces/IOracle.sol"; +// import "https://github.com/galadriel-ai/contracts/blob/main/contracts/contracts/interfaces/IOracle.sol"; +import "./interfaces/IOracle.sol"; contract SimpleLLM { - address private oracleAddress = ; // https://docs.galadriel.com/oracle-address + address private oracleAddress = 0x68EC9556830AD097D661Df2557FBCeC166a0A075; // use latest: https://docs.galadriel.com/oracle-address uint private runId = 0; string public message; string public response; From 2061b7e02164efd02e2964ca1d564aadb8138414 Mon Sep 17 00:00:00 2001 From: Karl Kallas Date: Thu, 27 Jun 2024 16:35:05 +0300 Subject: [PATCH 3/3] auto format --- contracts/contracts/AnthropicSimpleLLM.sol | 42 ++++++++++++---------- contracts/contracts/GroqSimpleLLM.sol | 38 +++++++++++--------- 2 files changed, 44 insertions(+), 36 deletions(-) diff --git a/contracts/contracts/AnthropicSimpleLLM.sol b/contracts/contracts/AnthropicSimpleLLM.sol index 92eb7e5..a567f05 100644 --- a/contracts/contracts/AnthropicSimpleLLM.sol +++ b/contracts/contracts/AnthropicSimpleLLM.sol @@ -13,19 +13,19 @@ contract SimpleLLM { constructor() { config = IOracle.LlmRequest({ - model : "claude-3-5-sonnet-20240620", // "claude-3-5-sonnet-20240620", "claude-3-opus-20240229", "claude-3-sonnet-20240229", "claude-3-haiku-20240307", "claude-2.1", "claude-2.0", "claude-instant-1.2" - frequencyPenalty : 21, // > 20 for null - logitBias : "", // empty str for null - maxTokens : 1000, // 0 for null - presencePenalty : 21, // > 20 for null - responseFormat : "{\"type\":\"text\"}", - seed : 0, // null - stop : "", // null - temperature : 10, // Example temperature (scaled up, 10 means 1.0), > 20 means null - topP : 101, // Percentage 0-100, > 100 means null - tools : "", - toolChoice : "auto", // "none" or "auto" - user : "" // null + model: "claude-3-5-sonnet-20240620", // "claude-3-5-sonnet-20240620", "claude-3-opus-20240229", "claude-3-sonnet-20240229", "claude-3-haiku-20240307", "claude-2.1", "claude-2.0", "claude-instant-1.2" + frequencyPenalty: 21, // > 20 for null + logitBias: "", // empty str for null + maxTokens: 1000, // 0 for null + presencePenalty: 21, // > 20 for null + responseFormat: '{"type":"text"}', + seed: 0, // null + stop: "", // null + temperature: 10, // Example temperature (scaled up, 10 means 1.0), > 20 means null + topP: 101, // Percentage 0-100, > 100 means null + tools: "", + toolChoice: "auto", // "none" or "auto" + user: "" // null }); } @@ -41,15 +41,18 @@ contract SimpleLLM { message = _message; IOracle(oracleAddress).createLlmCall(runId, config); } - - // required for Oracle - function onOracleLlmResponse( + + // required for Oracle + function onOracleLlmResponse( uint /*_runId*/, IOracle.LlmResponse memory _response, string memory _errorMessage ) public { require(msg.sender == oracleAddress, "Caller is not oracle"); - if (keccak256(abi.encodePacked(_errorMessage)) != keccak256(abi.encodePacked(""))) { + if ( + keccak256(abi.encodePacked(_errorMessage)) != + keccak256(abi.encodePacked("")) + ) { response = _errorMessage; } else { response = _response.content; @@ -57,7 +60,9 @@ contract SimpleLLM { } // required for Oracle - function getMessageHistory(uint /*_runId*/) public view returns (IOracle.Message[] memory) { + function getMessageHistory( + uint /*_runId*/ + ) public view returns (IOracle.Message[] memory) { IOracle.Message memory newMessage = IOracle.Message({ role: "user", content: new IOracle.Content[](1) @@ -71,4 +76,3 @@ contract SimpleLLM { return newMessages; } } - diff --git a/contracts/contracts/GroqSimpleLLM.sol b/contracts/contracts/GroqSimpleLLM.sol index d5f19ca..e9535f3 100644 --- a/contracts/contracts/GroqSimpleLLM.sol +++ b/contracts/contracts/GroqSimpleLLM.sol @@ -13,17 +13,17 @@ contract SimpleLLM { constructor() { config = IOracle.GroqRequest({ - model : "mixtral-8x7b-32768", // "llama3-8b-8192", "llama3-70b-8192", "mixtral-8x7b-32768" or "gemma-7b-it" - frequencyPenalty : 21, // > 20 for null - logitBias : "", // empty str for null - maxTokens : 1000, // 0 for null - presencePenalty : 21, // > 20 for null - responseFormat : "{\"type\":\"text\"}", - seed : 0, // null - stop : "", // null - temperature : 10, // Example temperature (scaled up, 10 means 1.0), > 20 means null - topP : 101, // Percentage 0-100, > 100 means null - user : "" // null + model: "mixtral-8x7b-32768", // "llama3-8b-8192", "llama3-70b-8192", "mixtral-8x7b-32768" or "gemma-7b-it" + frequencyPenalty: 21, // > 20 for null + logitBias: "", // empty str for null + maxTokens: 1000, // 0 for null + presencePenalty: 21, // > 20 for null + responseFormat: '{"type":"text"}', + seed: 0, // null + stop: "", // null + temperature: 10, // Example temperature (scaled up, 10 means 1.0), > 20 means null + topP: 101, // Percentage 0-100, > 100 means null + user: "" // null }); } @@ -39,15 +39,18 @@ contract SimpleLLM { message = _message; IOracle(oracleAddress).createGroqLlmCall(runId, config); } - - // required for Oracle - function onOracleGroqLlmResponse( + + // required for Oracle + function onOracleGroqLlmResponse( uint /*_runId*/, IOracle.GroqResponse memory _response, string memory _errorMessage ) public { require(msg.sender == oracleAddress, "Caller is not oracle"); - if (keccak256(abi.encodePacked(_errorMessage)) != keccak256(abi.encodePacked(""))) { + if ( + keccak256(abi.encodePacked(_errorMessage)) != + keccak256(abi.encodePacked("")) + ) { response = _errorMessage; } else { response = _response.content; @@ -55,7 +58,9 @@ contract SimpleLLM { } // required for Oracle - function getMessageHistory(uint /*_runId*/) public view returns (IOracle.Message[] memory) { + function getMessageHistory( + uint /*_runId*/ + ) public view returns (IOracle.Message[] memory) { IOracle.Message memory newMessage = IOracle.Message({ role: "user", content: new IOracle.Content[](1) @@ -69,4 +74,3 @@ contract SimpleLLM { return newMessages; } } -