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

Gemini docs : The multi-turn conversations example needs a slight modification #250

Closed
ImadSaddik opened this issue Feb 4, 2024 · 4 comments

Comments

@ImadSaddik
Copy link

The following code was taken from the Quickstart tutorial, that shows how to use Gemini in Android. In the multi-turn conversations example, I faced a problem when running the provided code snippet.

// For text-only input, use the gemini-pro model
GenerativeModel gm = new GenerativeModel(/* modelName */ "gemini-pro",
// Access your API key as a Build Configuration variable (see "Set up your API key" above)
    /* apiKey */ BuildConfig.apiKey);
GenerativeModelFutures model = GenerativeModelFutures.from(gm);

// (optional) Create previous chat history for context
Content.Builder userContentBuilder = new Content.Builder();
userContentBuilder.setRole("user");
userContentBuilder.addText("Hello, I have 2 dogs in my house.");
Content userContent = userContentBuilder.build();

Content.Builder modelContentBuilder = new Content.Builder();
modelContentBuilder.setRole("model");
modelContentBuilder.addText("Great to meet you. What would you like to know?");
Content modelContent = userContentBuilder.build();

List<Content> history = Arrays.asList(userContent, modelContent);

// Initialize the chat
ChatFutures chat = model.startChat(history);

// Create a new user message
Content userMessage = new Content.Builder()
    .addText("How many paws are in my house?")
    .build();

Executor executor = // ...

// Send the message
ListenableFuture<GenerateContentResponse> response = chat.sendMessage(userMessage);

Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
    @Override
    public void onSuccess(GenerateContentResponse result) {
        String resultText = result.getText();
        System.out.println(resultText);
    }

    @Override
    public void onFailure(Throwable t) {
        t.printStackTrace();
    }
}, executor);

When using the code as is, I get an Exception and to solve the problem. We should specify the user role when creating the userMessage.

So instead of this :

// Create a new user message
Content userMessage = new Content.Builder()
    .addText("How many paws are in my house?")
    .build();

It should be replaced by :

// Create a new user message
Content.Builder userMessageBuilder = new Content.Builder();
userMessageBuilder.setRole("user");
userMessageBuilder.addText(query);
Content userMessage = userMessageBuilder.build();
@thatfiredev
Copy link
Member

@ImadSaddik Sorry that you ran into that, but I'm glad that you managed to find the solution - and thanks for calling it out! We have now added setRole("user") to the multi-turn conversations example. :)

@ImadSaddik
Copy link
Author

Awesome 😎

@MohammedAbidNafi
Copy link

MohammedAbidNafi commented May 31, 2024

Whenever I add setRole its actually returning error and build() is not getting detected and if I write
Content userMessage = new Content.Builder() .setRole("user") .addText("How many paws are in my house?") .build();

This way then addText is not detected

@thatfiredev
Copy link
Member

@MohammedAbidNafi That's because these methods don't return a builder. You'll have to use them exactly as the snippet above:

Content.Builder userMessageBuilder = new Content.Builder();
userMessageBuilder.setRole("user");
userMessageBuilder.addText("How many paws are in my house?");
Content userMessage = userMessageBuilder.build();

Java support has not been prioritized in this SDK, unfortunately, so we ended up with these uncovenient APIs :/
I will discuss with the team about improving Java support.

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

No branches or pull requests

3 participants