-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
91 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |