Skip to content

Commit

Permalink
Remove RestSharp Code
Browse files Browse the repository at this point in the history
  • Loading branch information
angrave committed Jan 11, 2024
1 parent 41ebe4e commit e4b413a
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 89 deletions.
90 changes: 1 addition & 89 deletions TestRemoteLLM/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// See https://aka.ms/new-console-template for more information
using RestSharp;
// using RestSharp;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Diagnostics;
Expand Down Expand Up @@ -98,92 +98,4 @@ static async Task Main(string[] args)

}

private static void BadLLamaAPI(string[] args)
{
// As of Jan 10, Llama API is broken
// i) Images are ignored ii) Messing aroud with stop parameter is required,otherwise the server crashes (and ignores future api requests)

// System.Environment.GetEnvironmentVariable("AZURE_SUBSCRIPTION_KEYS") ?? defaultKeys
string fullPath = "../../../" + "dieselsubmarine.jpg"; // ClassTranscribeStudentsUse2020.png";

var bytes = File.ReadAllBytes(fullPath);
string imageBytesAsBase64 = Convert.ToBase64String(bytes);
//string mimetype = "image/png";

// var image = SKBitmap.Decode(bytes);
// Console.WriteLine($"Image ${fullPath} loaded. Dimensions: ${image.Width} x ${image.Height}");

//var CONTEXT = "A chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions.\n";
//var prompt = CONTEXT + "What doe image convey [img-1]?";
//var prompt = "USER:[img-12]Describe the image in detail.\nASSISTANT:";
var msg = "Describe this image.";
var prompt = $"A chat between a curious human and an artificial intelligence assistant.The assistant gives helpful, detailed, and polite answers to the human's questions.\nUSER:[img-10]{msg}\nASSISTANT:";

string model = "llava-v1.5-7b-Q4_K.gguf";
// "llava-v1.5-7b-Q4_K.gguf"; /* Verified using unzip -t AND network content*/


var userRole1 = new JObject { { "role", "user" }, { "content", "Write 2 truthful sentences." } };
var userRole2 = new JObject { { "role", "user" }, { "content", "tell me history of canada" } };
var userRole3 = new JObject { { "role", "user" }, { "content", prompt } };

// https://github.com/Mozilla-Ocho/llamafile/blob/main/llama.cpp/server/README.md#api-endpoints
// An array of objects to hold base64-encoded image data and its ids to be reference in prompt.
// You can determine the place of the image in the prompt as in the following: USER:[img-12]Describe the image in detail.\nASSISTANT:
// In this case, [img-12] will be replaced by the embeddings of the image id 12 in the following image_data array:
// {..., "image_data": [{"data": "<BASE64_STRING>", "id": 12}]}.

JObject image12 = new JObject
{
{"data", imageBytesAsBase64 }, {"id",10}
};
JObject requestJson = new JObject
{
{ "model", model}, // "llava-v1.5-7b-Q4_K.GGUF" },
// { "stop" , null},
{ "mode", "instruct" },
{ "image_data", new JArray { image12 } },
{ "messages", new JArray {
// systemRole,
userRole3
}
}
};

string requestJsonAsString = requestJson.ToString();
// Console.WriteLine(requestJsonAsString);


string LLMBASE = "http://localhost:8965/";
var authKey = "Nokey";

var clientOptions = new RestClientOptions
{
BaseUrl = new Uri(LLMBASE)
};
var client = new RestClient(clientOptions, null, null, true /*Enable simple factory */);
var request = new RestRequest("v1/chat/completions", Method.Post);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", $"Bearer {authKey}");
request.AddJsonBody(requestJsonAsString);

// request.AddJsonBody(requestJsonAsString);
// Todo: Are these even required?
// https://restsharp.dev/usage.html#get-or-post
// Put or Post ... Also, the request will be sent as application/x-www-form-urlencoded.

// In both cases, name and value will automatically be url - encoded.
//request.AddHeader("content-type", "application/x-www-form-urlencoded");

RestResponse response = client.Execute(request); // may throw exception
Console.WriteLine($"ResponseStatus: {response.ResponseStatus}");
Console.WriteLine($"Status Code: {response.StatusCode}");
Console.WriteLine($"Content: {response.Content}");
if (response.Content != null)
{
var responseAsJson = JObject.Parse(response.Content);
var responseContent = responseAsJson["choices"][0]["message"]["content"];
Console.WriteLine(responseContent);
}
}
}
90 changes: 90 additions & 0 deletions TestRemoteLLM/RemoteAPI.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// using RestSharp;

private static void BadLLamaAPI(string[] args)
{
// As of Jan 10, Llama API is broken
// i) Images are ignored ii) Messing aroud with stop parameter is required,otherwise the server crashes (and ignores future api requests)

// System.Environment.GetEnvironmentVariable("AZURE_SUBSCRIPTION_KEYS") ?? defaultKeys
string fullPath = "../../../" + "dieselsubmarine.jpg"; // ClassTranscribeStudentsUse2020.png";

var bytes = File.ReadAllBytes(fullPath);
string imageBytesAsBase64 = Convert.ToBase64String(bytes);
//string mimetype = "image/png";

// var image = SKBitmap.Decode(bytes);
// Console.WriteLine($"Image ${fullPath} loaded. Dimensions: ${image.Width} x ${image.Height}");

//var CONTEXT = "A chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions.\n";
//var prompt = CONTEXT + "What doe image convey [img-1]?";
//var prompt = "USER:[img-12]Describe the image in detail.\nASSISTANT:";
var msg = "Describe this image.";
var prompt = $"A chat between a curious human and an artificial intelligence assistant.The assistant gives helpful, detailed, and polite answers to the human's questions.\nUSER:[img-10]{msg}\nASSISTANT:";

string model = "llava-v1.5-7b-Q4_K.gguf";
// "llava-v1.5-7b-Q4_K.gguf"; /* Verified using unzip -t AND network content*/


var userRole1 = new JObject { { "role", "user" }, { "content", "Write 2 truthful sentences." } };
var userRole2 = new JObject { { "role", "user" }, { "content", "tell me history of canada" } };
var userRole3 = new JObject { { "role", "user" }, { "content", prompt } };

// https://github.com/Mozilla-Ocho/llamafile/blob/main/llama.cpp/server/README.md#api-endpoints
// An array of objects to hold base64-encoded image data and its ids to be reference in prompt.
// You can determine the place of the image in the prompt as in the following: USER:[img-12]Describe the image in detail.\nASSISTANT:
// In this case, [img-12] will be replaced by the embeddings of the image id 12 in the following image_data array:
// {..., "image_data": [{"data": "<BASE64_STRING>", "id": 12}]}.

JObject image12 = new JObject
{
{"data", imageBytesAsBase64 }, {"id",10}
};
JObject requestJson = new JObject
{
{ "model", model}, // "llava-v1.5-7b-Q4_K.GGUF" },
// { "stop" , null},
{ "mode", "instruct" },
{ "image_data", new JArray { image12 } },
{ "messages", new JArray {
// systemRole,
userRole3
}
}
};

string requestJsonAsString = requestJson.ToString();
// Console.WriteLine(requestJsonAsString);


string LLMBASE = "http://localhost:8965/";
var authKey = "Nokey";

var clientOptions = new RestClientOptions
{
BaseUrl = new Uri(LLMBASE)
};
var client = new RestClient(clientOptions, null, null, true /*Enable simple factory */);
var request = new RestRequest("v1/chat/completions", Method.Post);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", $"Bearer {authKey}");
request.AddJsonBody(requestJsonAsString);

// request.AddJsonBody(requestJsonAsString);
// Todo: Are these even required?
// https://restsharp.dev/usage.html#get-or-post
// Put or Post ... Also, the request will be sent as application/x-www-form-urlencoded.

// In both cases, name and value will automatically be url - encoded.
//request.AddHeader("content-type", "application/x-www-form-urlencoded");

RestResponse response = client.Execute(request); // may throw exception
Console.WriteLine($"ResponseStatus: {response.ResponseStatus}");
Console.WriteLine($"Status Code: {response.StatusCode}");
Console.WriteLine($"Content: {response.Content}");
if (response.Content != null)
{
var responseAsJson = JObject.Parse(response.Content);
var responseContent = responseAsJson["choices"][0]["message"]["content"];
Console.WriteLine(responseContent);
}
}

0 comments on commit e4b413a

Please sign in to comment.