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

feat: Add gemini sample #132

Merged
merged 11 commits into from
Dec 25, 2024
77 changes: 77 additions & 0 deletions samples/google/gemini/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
---
title: Gemini
description: Simple usage of the Gemini API
integrations: ["gemini"]
categories: ["Samples"]
---

# Gemini Sample

This [AutoKitteh](https://github.com/autokitteh/autokitteh) project
demonstrates integration with [Gemini](https://gemini.google.com).

It sends a couple of requests to the Gemini API, and prints the responses
back to the user.

API details:

- [Gemini API](https://ai.google.dev)
- [Python client library](https://github.com/google-gemini/generative-ai-python/blob/main/docs/api/google/generativeai.md)

## How It Works

1. Generate content using Gemini and log the response.
2. Conduct interactive chats with Gemini and log the responses.

## Deployment & Configuration

### Cloud Usage

- Initialize your connection with Gemini through the UI

daabr marked this conversation as resolved.
Show resolved Hide resolved
### Self-Hosted Server

#### Prerequisites

- [Install AutoKitteh](https://docs.autokitteh.com/get_started/install)

#### Installation Steps

1. Clone the repository:
```shell
git clone https://github.com/autokitteh/kittehub.git
cd kittehub/samples/google/gemini
```

2. Start the AutoKitteh server:
```shell
ak up --mode dev
```

3. Deploy the project:
```shell
ak deploy --manifest autokitteh.yaml
```

The output will show your connection IDs, which you'll need for the next step. Look for lines like:
```shell
[exec] create_connection "gemini_sample/gemini_connection": con_01je39d6frfdtshstfg5qpk8sz created
```

In this example, `con_01je39d6frfdtshstfg5qpk8sz` is the connection ID.

4. Initialize your connections using the CLI:
```shell
ak connection init gemini_connection <connection ID>
```

## Trigger Workflow

The workflow is triggered by sending an HTTP GET request.

> [!TIP]
> The workflow can also be triggered manually by clicking the "Run" button in the UI. Make sure to set the `on_http_get` function as the entrypoint.

> [!IMPORTANT]
> Ensure that the connection with Gemini is properly initialized before the workflow starts running.

15 changes: 15 additions & 0 deletions samples/google/gemini/autokitteh.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# This YAML file is a declarative manifest that describes the setup
# of an AutoKitteh project that demonstrates integration with Gemini.

version: v1

project:
name: gemini_sample
connections:
- name: gemini_conn
integration: googlegemini
triggers:
- name: on_http_get
type: webhook
event_type: get
call: program.py:on_http_get
34 changes: 34 additions & 0 deletions samples/google/gemini/program.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""Demonstration of AutoKitteh's Gemini integration.

A single entry-point function is implemented, it sends a couple of requests to the Gemini API, and prints the responses
daabr marked this conversation as resolved.
Show resolved Hide resolved
in the AutoKitteh session log.
"""

from autokitteh.google import gemini_client

MODEL = "gemini-1.5-flash"

gemini = gemini_client("gemini_conn", model_name=MODEL)


def on_http_get(event):
"""Entry-point function for handling HTTP GET requests in this workflow."""
# Example 1: trivial interaction with Gemini.
prompt = "say meow in different languages"
response = gemini.generate_content(prompt)
print(response.text)

# Example 2: interactive chat using the Gemini.
chat = gemini.start_chat(
history=[
{"role": "user", "parts": "Hello"},
{
"role": "model",
"parts": "Great to meet you. What would you like to know?",
},
]
)
response = chat.send_message("I have 2 cats in my house.")
print(response.text)
response = chat.send_message("How many paws are in my house?")
print(response.text)
Loading