matlab-deep-learning / llms-with-matlab

Connect MATLAB to LLM APIs, including OpenAI® Chat Completions, Azure® OpenAI Services, and Ollama™
Other
94 stars 20 forks source link

When does llms-with-matlab support Google Gemini? #4

Open sunweihunu opened 8 months ago

sunweihunu commented 8 months ago

When does llms-with-matlab support Google Gemini?

debymf commented 8 months ago

Thanks for the suggestion! We'll consider this as we move forward with our updates.

toshiakit commented 7 months ago

Here is an example you can try. I am assuming that you have your API key stored in the "gemini_api_key.env" file.

GEMINI_API_KEY=your_key

Here is a very simple MATLAB code.

loadenv("gemini_api_key.env")

prompt = "Tell me 5 jokes";
response = geminiGenerateContent(prompt);
if response.StatusCode == "OK"
    response.Body.Data.candidates.content.parts.text
else
    response.Body.Data.error
end

the function to call

function response = geminiGenerateContent(prompt, nvp)
    arguments
        prompt      (1,1) {mustBeTextScalar,mustBeNonempty}
        nvp.image   (1,1) {mustBeTextScalar} = "";
    end

    if strlength(nvp.image) == 0
        model = "gemini-pro";
        query = struct("contents",[]);
        query.contents = {struct("parts",[])};
        query.contents{1}.parts{1} = {struct("text",prompt)};
    else
        model = "gemini-pro-vision";
        if contains(nvp.image,["http://","https://"])
            imdata = imread(nvp.image);
            imwrite(imdata,"imdata.png")
            img = "imdata.png";
        else
            img = nvp.image;
        end
        fid = fopen(img);
        im = fread(fid,'*uint8');
        fclose(fid);
        b64 = matlab.net.base64encode(im);
        [~,~,ext] = fileparts(img);
        MIMEType = "image/" + erase(ext,".");
        query = struct("contents",[]);
        query.contents = {struct("parts",[])};
        query.contents{1}.parts = {struct("text",prompt),struct("inline_data",[])};
        query.contents{1}.parts{2}.inline_data = struct("mime_type",MIMEType,"data",[]);
        query.contents{1}.parts{2}.inline_data.data = b64;
        if isfile("imdata.png")
            delete("imdata.png")
        end
    end
    % endpoint = "https://generativelanguage.googleapis.com/v1beta/";
    endpoint = "https://generativelanguage.googleapis.com/v1beta/";
    method = "generateContent";

    import matlab.net.*
    import matlab.net.http.*  
    apikey = getenv("GEMINI_API_KEY");
    headers = HeaderField('Content-Type', 'application/json');
    request = RequestMessage('post', headers, query);
    response = send(request, URI(endpoint + ...
        "models/" + model + ...
        ":" + method + ...
        "?key=" + apikey));
end