Skip to content

Commit

Permalink
Add context
Browse files Browse the repository at this point in the history
WIP
  • Loading branch information
tharun571 committed Jul 18, 2024
1 parent 2135e36 commit a51c414
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 38 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ build/

bld

# API Keys for LLM
*_api_key.txt
# LLM Implementation
*_api_key.txt
*_chat_history.txt
96 changes: 89 additions & 7 deletions src/xmagics/xassist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <iostream>
#include <nlohmann/json.hpp>
#include <string>
#include <sys/stat.h>
#include <unordered_set>

using json = nlohmann::json;
Expand Down Expand Up @@ -58,6 +59,78 @@ namespace xcpp
}
};

class ChatHistory
{
public:
static std::string chat(const std::string& model, const std::string& user, const std::string& cell)
{
return appendAndReadBack(model, user, "\"" + cell + "\"");
}

static std::string chat(const std::string& model, const std::string& user, const nlohmann::json& cell)
{
return appendAndReadBack(model, user, cell.dump());
}

static void refresh(const std::string& model)
{
std::string chatHistoryFilePath = model + "_chat_history.txt";
std::ofstream out(chatHistoryFilePath, std::ios::out);
}

private:

static std::string appendAndReadBack(const std::string& model, const std::string& user, const std::string& serializedCell)
{
std::string chatHistoryFilePath = model + "_chat_history.txt";
std::ofstream out;
bool isEmpty = isFileEmpty(chatHistoryFilePath);

out.open(chatHistoryFilePath, std::ios::app);
if (!out)
{
std::cerr << "Failed to open file for writing chat history for model " << model << std::endl;
return "";
}

if (!isEmpty)
{
out << ", ";
}

if(model == "gemini")
{
out << "{ \"role\": \"" << user << "\", \"parts\": [ { \"text\": " << serializedCell << "}]}\n";
}
else
{
out << "{ \"role\": \"" << user << "\", \"content\": " << serializedCell << "}\n";
}

out.close();

return readFileContent(chatHistoryFilePath);
}

static bool isFileEmpty(const std::string& filePath)
{
std::ifstream file(filePath, std::ios::ate); // Open the file at the end
if (!file) // If the file cannot be opened, it might not exist
{
return true; // Consider non-existent files as empty
}
return file.tellg() == 0;
}

static std::string readFileContent(const std::string& filePath)
{
std::ifstream in(filePath);
std::stringstream bufferStream;
bufferStream << in.rdbuf();
return bufferStream.str();
}
};

class CurlHelper
{
private:
Expand Down Expand Up @@ -132,9 +205,10 @@ namespace xcpp
std::string gemini(const std::string& cell, const std::string& key)
{
CurlHelper curlHelper;
const std::string chatMessage = xcpp::ChatHistory::chat("gemini", "user", cell);
const std::string url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key="
+ key;
const std::string postData = R"({"contents": [{"parts":[{"text": ")" + cell + R"("}]}]})";
const std::string postData = R"({"contents": [ )" + chatMessage + R"(]})";

std::string response = curlHelper.performRequest(url, postData);

Expand All @@ -145,19 +219,21 @@ namespace xcpp
return "";
}

const std::string chat = xcpp::ChatHistory::chat("gemini", "model", j["candidates"][0]["content"]["parts"][0]["text"]);

return j["candidates"][0]["content"]["parts"][0]["text"];
}

std::string openai(const std::string& cell, const std::string& key)
{
CurlHelper curlHelper;
const std::string url = "https://api.openai.com/v1/chat/completions";
const std::string chatMessage = xcpp::ChatHistory::chat("openai", "user", cell);
const std::string postData = R"({
"model": "gpt-3.5-turbo-16k",
"messages": [{"role": "user", "content": ")"
+ cell + R"("}],
"temperature": 0.7
})";
"model": "gpt-3.5-turbo-16k",
"messages": [)" + chatMessage + R"(],
"temperature": 0.7
})";
std::string authHeader = "Authorization: Bearer " + key;

std::string response = curlHelper.performRequest(url, postData, authHeader);
Expand All @@ -170,6 +246,8 @@ namespace xcpp
return "";
}

const std::string chat = xcpp::ChatHistory::chat("openai", "assistant", j["choices"][0]["message"]["content"]);

return j["choices"][0]["message"]["content"];
}

Expand All @@ -192,11 +270,15 @@ namespace xcpp
return;
}

APIKeyManager api;
if (tokens[2] == "--save-key")
{
xcpp::APIKeyManager::saveApiKey(model, cell);
return;
}
else if (tokens[2] == "--refresh")
{
xcpp::ChatHistory::refresh(model);
return;
}

std::string key = xcpp::APIKeyManager::loadApiKey(model);
Expand Down
58 changes: 29 additions & 29 deletions test/test_interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -889,34 +889,34 @@ TEST_SUITE("xinspect"){
}
}

TEST_SUITE("xassist"){
// TEST_SUITE("xassist"){

TEST_CASE("model_not_found"){
xcpp::xassist assist;
std::string line = "%%xassist testModel";
std::string cell = "test input";
// TEST_CASE("model_not_found"){
// xcpp::xassist assist;
// std::string line = "%%xassist testModel";
// std::string cell = "test input";

StreamRedirectRAII redirect(std::cerr);
// StreamRedirectRAII redirect(std::cerr);

assist(line, cell);
// assist(line, cell);

REQUIRE(redirect.getCaptured() == "Model not found.\n");
// REQUIRE(redirect.getCaptured() == "Model not found.\n");

}
// }

TEST_CASE("gemini_save"){
xcpp::xassist assist;
std::string line = "%%xassist gemini --save-key";
std::string cell = "1234";
// TEST_CASE("gemini_save"){
// xcpp::xassist assist;
// std::string line = "%%xassist gemini --save-key";
// std::string cell = "1234";

assist(line, cell);
// assist(line, cell);

std::ifstream infile("gemini_api_key.txt");
std::string content;
std::getline(infile, content);
// std::ifstream infile("gemini_api_key.txt");
// std::string content;
// std::getline(infile, content);

REQUIRE(content == "1234");
infile.close();
// REQUIRE(content == "1234");
// infile.close();

// StreamRedirectRAII redirect(std::cerr);

Expand All @@ -925,19 +925,19 @@ TEST_SUITE("xassist"){
// REQUIRE(!redirect.getCaptured().empty());

// std::remove("gemini_api_key.txt");
}
// }

TEST_CASE("gemini"){
xcpp::xassist assist;
std::string line = "%%xassist gemini";
std::string cell = "hello";
// TEST_CASE("gemini"){
// xcpp::xassist assist;
// std::string line = "%%xassist gemini";
// std::string cell = "hello";

StreamRedirectRAII redirect(std::cerr);
// StreamRedirectRAII redirect(std::cerr);

assist(line, cell);
// assist(line, cell);

REQUIRE(!redirect.getCaptured().empty());
}
// REQUIRE(!redirect.getCaptured().empty());
// }

// TEST_CASE("openai"){
// xcpp::xassist assist;
Expand All @@ -962,4 +962,4 @@ TEST_SUITE("xassist"){
// std::remove("openai_api_key.txt");
// }

}
// }

0 comments on commit a51c414

Please sign in to comment.