Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sunweihunu opened this issue Dec 27, 2023 · 2 comments
Open

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

sunweihunu opened this issue Dec 27, 2023 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@sunweihunu
Copy link

When does llms-with-matlab support Google Gemini?

@debymf
Copy link
Member

debymf commented Jan 15, 2024

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

@debymf debymf self-assigned this Jan 15, 2024
@debymf debymf added the enhancement New feature or request label Jan 15, 2024
@toshiakit
Copy link
Collaborator

toshiakit commented Feb 14, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants