simonw / llm-gemini

LLM plugin to access Google's Gemini family of models
Apache License 2.0
104 stars 8 forks source link

Add support for system prompts #6

Closed simonw closed 5 months ago

simonw commented 5 months ago

Released today: https://developers.googleblog.com/2024/04/gemini-15-pro-in-public-preview-with-new-features.html

System instructions: Guide the model's responses with system instructions, now available in Google AI Studio and the Gemini API. Define roles, formats, goals, and rules to steer the model's behavior for your specific use case.

simonw commented 5 months ago

These are implemented in the Python client library now:

But the REST APi documentation doesn't seem to have been updated yet:

After quite some poking around I found this:

https://github.com/googleapis/google-cloud-python/blob/d9222a97786ce1badae4561410ca5e09386a3377/packages/google-ai-generativelanguage/google/ai/generativelanguage_v1beta/types/generative_service.py#L81-L156

Which included this chunk of code:

    system_instruction: gag_content.Content = proto.Field(
        proto.MESSAGE,
        number=8,
        optional=True,
        message=gag_content.Content,
    )

I found gag_content.Content here: https://github.com/googleapis/google-cloud-python/blob/d9222a97786ce1badae4561410ca5e09386a3377/packages/google-ai-generativelanguage/google/ai/generativelanguage_v1/types/content.py#L32-L59

And after quite a bit of trial and error figured this out as the JSON recipe:

{
  "contents": [
    {
      "role": "user",
      "parts": [
        {
          "text": "say hi three times three different ways"
        }
      ]
    }
  ],
  "systemInstruction": {
    "parts": [
      {
        "text": "in spanish"
      }
    ]
  }
}
simonw commented 5 months ago
$ llm -m p15 'say hi three times three different ways' --system 'in spanish'
¡Hola! 👋 ¡Buenos días! ☀️ ¡Buenas tardes! 😊 

$ llm -m p15 'say hi three times three different ways' --system 'in german' 
Klar, hier sind drei verschiedene Arten, "Hallo" zu sagen:

* Grüß Gott!
* Servus!
* Hallo!