Skip to content

Commit

Permalink
Reverts back certain changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kgrofelnik committed Jun 26, 2024
1 parent 07fcc04 commit aae1448
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 314 deletions.
86 changes: 9 additions & 77 deletions contracts/contracts/ChatGpt.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,10 @@ contract ChatGpt {

// @notice Address of the oracle contract
address public oracleAddress;

// @notice Configuration for the LLM request
IOracle.LlmRequest private config;

// @notice CID of the knowledge base
string public knowledgeBase;

// @notice Mapping from chat ID to the tool currently running
mapping(uint => string) public toolRunning;

// @notice Event emitted when the oracle address is updated
event OracleAddressUpdated(address indexed newOracleAddress);

Expand All @@ -51,22 +45,6 @@ contract ChatGpt {
owner = msg.sender;
oracleAddress = initialOracleAddress;
knowledgeBase = knowledgeBaseCID;

config = IOracle.LlmRequest({
model : "claude-3-5-sonnet-20240620",
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 : "[{\"type\":\"function\",\"function\":{\"name\":\"web_search\",\"description\":\"Search the internet\",\"parameters\":{\"type\":\"object\",\"properties\":{\"query\":{\"type\":\"string\",\"description\":\"Search query\"}},\"required\":[\"query\"]}}},{\"type\":\"function\",\"function\":{\"name\":\"code_interpreter\",\"description\":\"Evaluates python code in a sandbox environment. The environment resets on every execution. You must send the whole script every time and print your outputs. Script should be pure python code that can be evaluated. It should be in python format NOT markdown. The code should NOT be wrapped in backticks. All python packages including requests, matplotlib, scipy, numpy, pandas, etc are available. Output can only be read from stdout, and stdin. Do not use things like plot.show() as it will not work. print() any output and results so you can capture the output.\",\"parameters\":{\"type\":\"object\",\"properties\":{\"code\":{\"type\":\"string\",\"description\":\"The pure python script to be evaluated. The contents will be in main.py. It should not be in markdown format.\"}},\"required\":[\"code\"]}}}]",
toolChoice : "auto", // "none" or "auto"
user : "" // null
});
}

// @notice Ensures the caller is the contract owner
Expand Down Expand Up @@ -114,7 +92,7 @@ contract ChatGpt {
);
} else {
// Otherwise, create an LLM call
IOracle(oracleAddress).createLlmCall(currentId, config);
IOracle(oracleAddress).createLlmCall(currentId);
}
emit ChatCreated(msg.sender, currentId);

Expand All @@ -127,8 +105,8 @@ contract ChatGpt {
// @dev Called by teeML oracle
function onOracleLlmResponse(
uint runId,
IOracle.LlmResponse memory response,
string memory errorMessage
string memory response,
string memory /*errorMessage*/
) public onlyOracle {
ChatRun storage run = chatRuns[runId];
require(
Expand All @@ -137,48 +115,10 @@ contract ChatGpt {
);

Message memory newMessage;
if (!compareStrings(errorMessage, "")) {
newMessage.role = "assistant";
newMessage.content = errorMessage;
run.messages.push(newMessage);
run.messagesCount++;
} else {
if (!compareStrings(response.functionName, "")) {
toolRunning[runId] = response.functionName;
IOracle(oracleAddress).createFunctionCall(runId, response.functionName, response.functionArguments);
} else {
toolRunning[runId] = "";
}
newMessage.role = "assistant";
newMessage.content = response.content;
run.messages.push(newMessage);
run.messagesCount++;
}
}

// @notice Handles the response from the oracle for a function call
// @param runId The ID of the chat run
// @param response The response from the oracle
// @param errorMessage Any error message
// @dev Called by teeML oracle
function onOracleFunctionResponse(
uint runId,
string memory response,
string memory errorMessage
) public onlyOracle {
require(
!compareStrings(toolRunning[runId], ""),
"No function to respond to"
);
ChatRun storage run = chatRuns[runId];
if (compareStrings(errorMessage, "")) {
Message memory newMessage;
newMessage.role = "user";
newMessage.content = response;
run.messages.push(newMessage);
run.messagesCount++;
IOracle(oracleAddress).createLlmCall(runId, config);
}
newMessage.content = response;
newMessage.role = "assistant";
run.messages.push(newMessage);
run.messagesCount++;
}

// @notice Handles the response from the oracle for a knowledge base query
Expand Down Expand Up @@ -215,7 +155,7 @@ contract ChatGpt {
lastMessage.content = newContent;

// Call LLM
IOracle(oracleAddress).createLlmCall(runId, config);
IOracle(oracleAddress).createLlmCall(runId);
}

// @notice Adds a new message to an existing chat run
Expand Down Expand Up @@ -246,7 +186,7 @@ contract ChatGpt {
);
} else {
// Otherwise, create an LLM call
IOracle(oracleAddress).createLlmCall(runId, config);
IOracle(oracleAddress).createLlmCall(runId);
}
}

Expand All @@ -273,12 +213,4 @@ contract ChatGpt {
}
return roles;
}

// @notice Compares two strings for equality
// @param a The first string
// @param b The second string
// @return True if the strings are equal, false otherwise
function compareStrings(string memory a, string memory b) private pure returns (bool) {
return (keccak256(abi.encodePacked((a))) == keccak256(abi.encodePacked((b))));
}
}
52 changes: 8 additions & 44 deletions contracts/contracts/GroqChatGpt.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,19 @@ contract GroqChatGpt {
// @notice Address of the oracle contract
address public oracleAddress;

// @notice Mapping from chat ID to the tool currently running
mapping(uint => string) public toolRunning;

// @notice Event emitted when the oracle address is updated
event OracleAddressUpdated(address indexed newOracleAddress);

// @notice Configuration for the LLM request
IOracle.LlmRequest private config;
// @notice Configuration for the Groq request
IOracle.GroqRequest private config;

// @param initialOracleAddress Initial address of the oracle contract
constructor(address initialOracleAddress) {
owner = msg.sender;
oracleAddress = initialOracleAddress;
chatRunsCount = 0;

config = IOracle.LlmRequest({
config = IOracle.GroqRequest({
model : "mixtral-8x7b-32768",
frequencyPenalty : 21, // > 20 for null
logitBias : "", // empty str for null
Expand All @@ -59,8 +56,6 @@ contract GroqChatGpt {
stop : "", // null
temperature : 10, // Example temperature (scaled up, 10 means 1.0), > 20 means null
topP : 101, // Percentage 0-100, > 100 means null
tools : "[{\"type\":\"function\",\"function\":{\"name\":\"web_search\",\"description\":\"Search the internet\",\"parameters\":{\"type\":\"object\",\"properties\":{\"query\":{\"type\":\"string\",\"description\":\"Search query\"}},\"required\":[\"query\"]}}},{\"type\":\"function\",\"function\":{\"name\":\"code_interpreter\",\"description\":\"Evaluates python code in a sandbox environment. The environment resets on every execution. You must send the whole script every time and print your outputs. Script should be pure python code that can be evaluated. It should be in python format NOT markdown. The code should NOT be wrapped in backticks. All python packages including requests, matplotlib, scipy, numpy, pandas, etc are available. Output can only be read from stdout, and stdin. Do not use things like plot.show() as it will not work. print() any output and results so you can capture the output.\",\"parameters\":{\"type\":\"object\",\"properties\":{\"code\":{\"type\":\"string\",\"description\":\"The pure python script to be evaluated. The contents will be in main.py. It should not be in markdown format.\"}},\"required\":[\"code\"]}}}]",
toolChoice : "auto", // "none" or "auto"
user : "" // null
});
}
Expand Down Expand Up @@ -99,20 +94,20 @@ contract GroqChatGpt {
uint currentId = chatRunsCount;
chatRunsCount = chatRunsCount + 1;

IOracle(oracleAddress).createLlmCall(currentId, config);
IOracle(oracleAddress).createGroqLlmCall(currentId, config);
emit ChatCreated(msg.sender, currentId);

return currentId;
}

// @notice Handles the response from the oracle for an LLM call
// @notice Handles the response from the oracle for a Groq LLM call
// @param runId The ID of the chat run
// @param response The response from the oracle
// @param errorMessage Any error message
// @dev Called by teeML oracle
function onOracleLlmResponse(
function onOracleGroqLlmResponse(
uint runId,
IOracle.LlmResponse memory response,
IOracle.GroqResponse memory response,
string memory errorMessage
) public onlyOracle {
ChatRun storage run = chatRuns[runId];
Expand All @@ -127,12 +122,6 @@ contract GroqChatGpt {
run.messages.push(newMessage);
run.messagesCount++;
} else {
if (!compareStrings(response.functionName, "")) {
toolRunning[runId] = response.functionName;
IOracle(oracleAddress).createFunctionCall(runId, response.functionName, response.functionArguments);
} else {
toolRunning[runId] = "";
}
Message memory newMessage;
newMessage.role = "assistant";
newMessage.content = response.content;
Expand All @@ -141,31 +130,6 @@ contract GroqChatGpt {
}
}

// @notice Handles the response from the oracle for a function call
// @param runId The ID of the chat run
// @param response The response from the oracle
// @param errorMessage Any error message
// @dev Called by teeML oracle
function onOracleFunctionResponse(
uint runId,
string memory response,
string memory errorMessage
) public onlyOracle {
require(
!compareStrings(toolRunning[runId], ""),
"No function to respond to"
);
ChatRun storage run = chatRuns[runId];
if (compareStrings(errorMessage, "")) {
Message memory newMessage;
newMessage.role = "user";
newMessage.content = response;
run.messages.push(newMessage);
run.messagesCount++;
IOracle(oracleAddress).createLlmCall(runId, config);
}
}

// @notice Adds a new message to an existing chat run
// @param message The new message to add
// @param runId The ID of the chat run
Expand All @@ -185,7 +149,7 @@ contract GroqChatGpt {
run.messages.push(newMessage);
run.messagesCount++;

IOracle(oracleAddress).createLlmCall(runId, config);
IOracle(oracleAddress).createGroqLlmCall(runId, config);
}

// @notice Retrieves the message history contents of a chat run
Expand Down
65 changes: 30 additions & 35 deletions contracts/contracts/OpenAiChatGpt.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,35 +33,32 @@ contract OpenAiChatGpt {
// @notice Address of the oracle contract
address public oracleAddress;

// @notice Mapping from chat ID to the tool currently running
mapping(uint => string) public toolRunning;

// @notice Event emitted when the oracle address is updated
event OracleAddressUpdated(address indexed newOracleAddress);

// @notice Configuration for the LLM request
IOracle.LlmRequest private config;
// @notice Configuration for the OpenAI request
IOracle.OpenAiRequest private config;

// @param initialOracleAddress Initial address of the oracle contract
constructor(address initialOracleAddress) {
owner = msg.sender;
oracleAddress = initialOracleAddress;
chatRunsCount = 0;

config = IOracle.LlmRequest({
model : "gpt-4-turbo-preview",
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 : "[{\"type\":\"function\",\"function\":{\"name\":\"web_search\",\"description\":\"Search the internet\",\"parameters\":{\"type\":\"object\",\"properties\":{\"query\":{\"type\":\"string\",\"description\":\"Search query\"}},\"required\":[\"query\"]}}},{\"type\":\"function\",\"function\":{\"name\":\"code_interpreter\",\"description\":\"Evaluates python code in a sandbox environment. The environment resets on every execution. You must send the whole script every time and print your outputs. Script should be pure python code that can be evaluated. It should be in python format NOT markdown. The code should NOT be wrapped in backticks. All python packages including requests, matplotlib, scipy, numpy, pandas, etc are available. Output can only be read from stdout, and stdin. Do not use things like plot.show() as it will not work. print() any output and results so you can capture the output.\",\"parameters\":{\"type\":\"object\",\"properties\":{\"code\":{\"type\":\"string\",\"description\":\"The pure python script to be evaluated. The contents will be in main.py. It should not be in markdown format.\"}},\"required\":[\"code\"]}}}]",
toolChoice : "auto", // "none" or "auto"
user : "" // null
config = IOracle.OpenAiRequest({
model : "gpt-4-turbo-preview",
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 : "[{\"type\":\"function\",\"function\":{\"name\":\"web_search\",\"description\":\"Search the internet\",\"parameters\":{\"type\":\"object\",\"properties\":{\"query\":{\"type\":\"string\",\"description\":\"Search query\"}},\"required\":[\"query\"]}}},{\"type\":\"function\",\"function\":{\"name\":\"code_interpreter\",\"description\":\"Evaluates python code in a sandbox environment. The environment resets on every execution. You must send the whole script every time and print your outputs. Script should be pure python code that can be evaluated. It should be in python format NOT markdown. The code should NOT be wrapped in backticks. All python packages including requests, matplotlib, scipy, numpy, pandas, etc are available. Output can only be read from stdout, and stdin. Do not use things like plot.show() as it will not work. print() any output and results so you can capture the output.\",\"parameters\":{\"type\":\"object\",\"properties\":{\"code\":{\"type\":\"string\",\"description\":\"The pure python script to be evaluated. The contents will be in main.py. It should not be in markdown format.\"}},\"required\":[\"code\"]}}}]",
toolChoice : "auto", // "none" or "auto"
user : "" // null
});
}

Expand Down Expand Up @@ -100,20 +97,20 @@ contract OpenAiChatGpt {
uint currentId = chatRunsCount;
chatRunsCount = chatRunsCount + 1;

IOracle(oracleAddress).createLlmCall(currentId, config);
IOracle(oracleAddress).createOpenAiLlmCall(currentId, config);
emit ChatCreated(msg.sender, currentId);

return currentId;
}

// @notice Handles the response from the oracle for an LLM call
// @notice Handles the response from the oracle for an OpenAI LLM call
// @param runId The ID of the chat run
// @param response The response from the oracle
// @param errorMessage Any error message
// @dev Called by teeML oracle
function onOracleLlmResponse(
function onOracleOpenAiLlmResponse(
uint runId,
IOracle.LlmResponse memory response,
IOracle.OpenAiResponse memory response,
string memory errorMessage
) public onlyOracle {
ChatRun storage run = chatRuns[runId];
Expand All @@ -129,17 +126,15 @@ contract OpenAiChatGpt {
run.messages.push(newMessage);
run.messagesCount++;
} else {
if (!compareStrings(response.functionName, "")) {
toolRunning[runId] = response.functionName;
if (compareStrings(response.content, "")) {
IOracle(oracleAddress).createFunctionCall(runId, response.functionName, response.functionArguments);
} else {
toolRunning[runId] = "";
Message memory newMessage;
newMessage.role = "assistant";
newMessage.content = response.content;
run.messages.push(newMessage);
run.messagesCount++;
}
Message memory newMessage;
newMessage.role = "assistant";
newMessage.content = response.content;
run.messages.push(newMessage);
run.messagesCount++;
}
}

Expand All @@ -153,18 +148,18 @@ contract OpenAiChatGpt {
string memory response,
string memory errorMessage
) public onlyOracle {
ChatRun storage run = chatRuns[runId];
require(
!compareStrings(toolRunning[runId], ""),
compareStrings(run.messages[run.messagesCount - 1].role, "user"),
"No function to respond to"
);
ChatRun storage run = chatRuns[runId];
if (compareStrings(errorMessage, "")) {
Message memory newMessage;
newMessage.role = "user";
newMessage.content = response;
run.messages.push(newMessage);
run.messagesCount++;
IOracle(oracleAddress).createLlmCall(runId, config);
IOracle(oracleAddress).createOpenAiLlmCall(runId, config);
}
}

Expand All @@ -187,7 +182,7 @@ contract OpenAiChatGpt {
run.messages.push(newMessage);
run.messagesCount++;

IOracle(oracleAddress).createLlmCall(runId, config);
IOracle(oracleAddress).createOpenAiLlmCall(runId, config);
}

// @notice Retrieves the message history contents of a chat run
Expand Down
Loading

0 comments on commit aae1448

Please sign in to comment.