Skip to content

Commit

Permalink
New history format
Browse files Browse the repository at this point in the history
  • Loading branch information
kgrofelnik committed Jul 2, 2024
1 parent 3df7d47 commit 40badf1
Show file tree
Hide file tree
Showing 12 changed files with 157 additions and 248 deletions.
67 changes: 25 additions & 42 deletions contracts/contracts/Agent.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,9 @@ contract Agent {

string public prompt;

struct Message {
string role;
string content;
}

struct AgentRun {
address owner;
Message[] messages;
IOracle.Message[] messages;
uint responsesCount;
uint8 max_iterations;
bool is_finished;
Expand Down Expand Up @@ -102,14 +97,10 @@ contract Agent {
run.responsesCount = 0;
run.max_iterations = max_iterations;

Message memory systemMessage;
systemMessage.content = prompt;
systemMessage.role = "system";
IOracle.Message memory systemMessage = createTextMessage("system", prompt);
run.messages.push(systemMessage);

Message memory newMessage;
newMessage.content = query;
newMessage.role = "user";
IOracle.Message memory newMessage = createTextMessage("user", query);
run.messages.push(newMessage);

uint currentId = agentRunCount;
Expand All @@ -134,9 +125,7 @@ contract Agent {
AgentRun storage run = agentRuns[runId];

if (!compareStrings(errorMessage, "")) {
Message memory newMessage;
newMessage.role = "assistant";
newMessage.content = errorMessage;
IOracle.Message memory newMessage = createTextMessage("assistant", errorMessage);
run.messages.push(newMessage);
run.responsesCount++;
run.is_finished = true;
Expand All @@ -147,10 +136,8 @@ contract Agent {
return;
}
if (!compareStrings(response.content, "")) {
Message memory assistantMessage;
assistantMessage.content = response.content;
assistantMessage.role = "assistant";
run.messages.push(assistantMessage);
IOracle.Message memory newMessage = createTextMessage("assistant", response.content);
run.messages.push(newMessage);
run.responsesCount++;
}
if (!compareStrings(response.functionName, "")) {
Expand Down Expand Up @@ -178,36 +165,18 @@ contract Agent {
result = errorMessage;
}

Message memory newMessage;
newMessage.role = "user";
newMessage.content = result;
IOracle.Message memory newMessage = createTextMessage("user", result);
run.messages.push(newMessage);
run.responsesCount++;
IOracle(oracleAddress).createOpenAiLlmCall(runId, config);
}

// @notice Retrieves the message history contents for a given agent run
// @param agentId The ID of the agent run
// @return An array of message contents
// @dev Called by teeML oracle
function getMessageHistoryContents(uint agentId) public view returns (string[] memory) {
string[] memory messages = new string[](agentRuns[agentId].messages.length);
for (uint i = 0; i < agentRuns[agentId].messages.length; i++) {
messages[i] = agentRuns[agentId].messages[i].content;
}
return messages;
}

// @notice Retrieves the roles of the messages in a given agent run
// @notice Retrieves the message history for a given agent run
// @param agentId The ID of the agent run
// @return An array of message roles
// @return An array of messages
// @dev Called by teeML oracle
function getMessageHistoryRoles(uint agentId) public view returns (string[] memory) {
string[] memory roles = new string[](agentRuns[agentId].messages.length);
for (uint i = 0; i < agentRuns[agentId].messages.length; i++) {
roles[i] = agentRuns[agentId].messages[i].role;
}
return roles;
function getMessageHistory(uint agentId) public view returns (IOracle.Message[] memory) {
return agentRuns[agentId].messages;
}

// @notice Checks if a given agent run is finished
Expand All @@ -217,6 +186,20 @@ contract Agent {
return agentRuns[runId].is_finished;
}

// @notice Creates a text message with the given role and content
// @param role The role of the message
// @param content The content of the message
// @return The created message
function createTextMessage(string memory role, string memory content) private pure returns (IOracle.Message memory) {
IOracle.Message memory newMessage = IOracle.Message({
role: role,
content: new IOracle.Content[](1)
});
newMessage.content[0].contentType = "text";
newMessage.content[0].value = content;
return newMessage;
}

// @notice Compares two strings for equality
// @param a The first string
// @param b The second string
Expand Down
49 changes: 19 additions & 30 deletions contracts/contracts/AnthropicChatGpt.sol
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,7 @@ contract AnthropicChatGpt {
ChatRun storage run = chatRuns[chatRunsCount];

run.owner = msg.sender;
IOracle.Message memory newMessage = IOracle.Message({
role: "user",
content: new IOracle.Content[](1)
});
newMessage.content[0].contentType = "text";
newMessage.content[0].value = message;
IOracle.Message memory newMessage = createTextMessage("user", message);
run.messages.push(newMessage);
run.messagesCount++;

Expand Down Expand Up @@ -122,12 +117,7 @@ contract AnthropicChatGpt {
);

if (!compareStrings(errorMessage, "")) {
IOracle.Message memory newMessage = IOracle.Message({
role: "assistant",
content: new IOracle.Content[](1)
});
newMessage.content[0].contentType = "text";
newMessage.content[0].value = errorMessage;
IOracle.Message memory newMessage = createTextMessage("assistant", errorMessage);
run.messages.push(newMessage);
run.messagesCount++;
} else {
Expand All @@ -137,12 +127,7 @@ contract AnthropicChatGpt {
} else {
toolRunning[runId] = "";
}
IOracle.Message memory newMessage = IOracle.Message({
role: "assistant",
content: new IOracle.Content[](1)
});
newMessage.content[0].contentType = "text";
newMessage.content[0].value = response.content;
IOracle.Message memory newMessage = createTextMessage("assistant", response.content);
run.messages.push(newMessage);
run.messagesCount++;
}
Expand All @@ -164,12 +149,7 @@ contract AnthropicChatGpt {
);
ChatRun storage run = chatRuns[runId];
if (compareStrings(errorMessage, "")) {
IOracle.Message memory newMessage = IOracle.Message({
role: "user",
content: new IOracle.Content[](1)
});
newMessage.content[0].contentType = "text";
newMessage.content[0].value = response;
IOracle.Message memory newMessage = createTextMessage("user", response);
run.messages.push(newMessage);
run.messagesCount++;
IOracle(oracleAddress).createLlmCall(runId, config);
Expand Down Expand Up @@ -226,12 +206,7 @@ contract AnthropicChatGpt {
run.owner == msg.sender, "Only chat owner can add messages"
);

IOracle.Message memory newMessage = IOracle.Message({
role: "user",
content: new IOracle.Content[](1)
});
newMessage.content[0].contentType = "text";
newMessage.content[0].value = message;
IOracle.Message memory newMessage = createTextMessage("user", message);
run.messages.push(newMessage);
run.messagesCount++;
// If there is a knowledge base, create a knowledge base query
Expand All @@ -256,6 +231,20 @@ contract AnthropicChatGpt {
return chatRuns[chatId].messages;
}

// @notice Creates a text message with the given role and content
// @param role The role of the message
// @param content The content of the message
// @return The created message
function createTextMessage(string memory role, string memory content) private pure returns (IOracle.Message memory) {
IOracle.Message memory newMessage = IOracle.Message({
role: role,
content: new IOracle.Content[](1)
});
newMessage.content[0].contentType = "text";
newMessage.content[0].value = content;
return newMessage;
}

// @notice Compares two strings for equality
// @param a The first string
// @param b The second string
Expand Down
11 changes: 1 addition & 10 deletions contracts/contracts/AnthropicSimpleLLM.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ contract SimpleLLM {
}

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);
}
Expand All @@ -50,8 +42,7 @@ contract SimpleLLM {
) public {
require(msg.sender == oracleAddress, "Caller is not oracle");
if (
keccak256(abi.encodePacked(_errorMessage)) !=
keccak256(abi.encodePacked(""))
bytes(_errorMessage).length > 0
) {
response = _errorMessage;
} else {
Expand Down
59 changes: 23 additions & 36 deletions contracts/contracts/ChatGpt.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,9 @@ import "./interfaces/IOracle.sol";
// @notice This contract handles chat interactions and integrates with teeML oracle for LLM and knowledge base queries.
contract ChatGpt {

struct Message {
string role;
string content;
}

struct ChatRun {
address owner;
Message[] messages;
IOracle.Message[] messages;
uint messagesCount;
}

Expand Down Expand Up @@ -73,9 +68,7 @@ contract ChatGpt {
ChatRun storage run = chatRuns[chatRunsCount];

run.owner = msg.sender;
Message memory newMessage;
newMessage.content = message;
newMessage.role = "user";
IOracle.Message memory newMessage = createTextMessage("user", message);
run.messages.push(newMessage);
run.messagesCount = 1;

Expand Down Expand Up @@ -114,9 +107,7 @@ contract ChatGpt {
"No message to respond to"
);

Message memory newMessage;
newMessage.content = response;
newMessage.role = "assistant";
IOracle.Message memory newMessage = createTextMessage("assistant", response);
run.messages.push(newMessage);
run.messagesCount++;
}
Expand All @@ -136,10 +127,10 @@ contract ChatGpt {
"No message to add context to"
);
// Retrieve the last user message
Message storage lastMessage = run.messages[run.messagesCount - 1];
IOracle.Message storage lastMessage = run.messages[run.messagesCount - 1];

// Start with the original message content
string memory newContent = lastMessage.content;
string memory newContent = lastMessage.content[0].value;

// Append "Relevant context:\n" only if there are documents
if (documents.length > 0) {
Expand All @@ -152,7 +143,7 @@ contract ChatGpt {
}

// Finally, set the lastMessage content to the newly constructed string
lastMessage.content = newContent;
lastMessage.content[0].value = newContent;

// Call LLM
IOracle(oracleAddress).createLlmCall(runId);
Expand All @@ -171,9 +162,7 @@ contract ChatGpt {
run.owner == msg.sender, "Only chat owner can add messages"
);

Message memory newMessage;
newMessage.content = message;
newMessage.role = "user";
IOracle.Message memory newMessage = createTextMessage("user", message);
run.messages.push(newMessage);
run.messagesCount++;
// If there is a knowledge base, create a knowledge base query
Expand All @@ -190,27 +179,25 @@ contract ChatGpt {
}
}

// @notice Retrieves the message history contents of a chat run
// @notice Retrieves the message history of a chat run
// @param chatId The ID of the chat run
// @return An array of message contents
// @return An array of messages
// @dev Called by teeML oracle
function getMessageHistoryContents(uint chatId) public view returns (string[] memory) {
string[] memory messages = new string[](chatRuns[chatId].messages.length);
for (uint i = 0; i < chatRuns[chatId].messages.length; i++) {
messages[i] = chatRuns[chatId].messages[i].content;
}
return messages;
function getMessageHistory(uint chatId) public view returns (IOracle.Message[] memory) {
return chatRuns[chatId].messages;
}

// @notice Retrieves the roles of the messages in a chat run
// @param chatId The ID of the chat run
// @return An array of message roles
// @dev Called by teeML oracle
function getMessageHistoryRoles(uint chatId) public view returns (string[] memory) {
string[] memory roles = new string[](chatRuns[chatId].messages.length);
for (uint i = 0; i < chatRuns[chatId].messages.length; i++) {
roles[i] = chatRuns[chatId].messages[i].role;
}
return roles;
// @notice Creates a text message with the given role and content
// @param role The role of the message
// @param content The content of the message
// @return The created message
function createTextMessage(string memory role, string memory content) private pure returns (IOracle.Message memory) {
IOracle.Message memory newMessage = IOracle.Message({
role: role,
content: new IOracle.Content[](1)
});
newMessage.content[0].contentType = "text";
newMessage.content[0].value = content;
return newMessage;
}
}
Loading

0 comments on commit 40badf1

Please sign in to comment.