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

Add Project IDX support #318

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions .idx/dev.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# To learn more about how to use Nix to configure your environment
# see: https://developers.google.com/idx/guides/customize-idx-env
{ pkgs, ... }: {
# Which nixpkgs channel to use.
channel = "stable-23.11"; # or "unstable"
# Use https://search.nixos.org/packages to find packages
packages = [
pkgs.python311
pkgs.python311Packages.pip
];
# Sets environment variables in the workspace
env = {
GOOGLE_API_KEY="TODO";
};
idx = {
# Search for the extensions you want on https://open-vsx.org/ and use "publisher.id"
extensions = [
"ms-toolsai.jupyter"
"ms-python.python"
"eamodio.gitlens"
];
workspace = {
# Runs when a workspace is first created with this `dev.nix` file
onCreate = {
create-venv = ''
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
python scripts/api-key.py
'';
# Open editors for the following files by default, if they exist:
default.openFiles = [ "main.ipynb" ];
};
# To run something each time the workspace is (re)started, use the `onStart` hook
};
# Enable previews and customize configuration
previews = {};
};
}
Binary file added .idx/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions .idx/integrations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"gemini_api": {}
}
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,28 @@ The Gemini API is a REST API. You can call the API using a command line tool lik
Ask a question on the [Google AI Developer Forum](https://discuss.ai.google.dev/).

## The Gemini API on Google Cloud Vertex AI
If you're an enterprise developer looking to build on a fully-managed platform, you can also use the Gemini API on Google Cloud. Check out this [repo](https://github.com/GoogleCloudPlatform/generative-ai) for lots of cool examples.
If you're an enterprise developer looking to build on a fully-managed platform, you can also use the Gemini API on Google Cloud. Check out this [repo](https://github.com/GoogleCloudPlatform/generative-ai) for lots of cool examples.

## Project IDX

<a href="https://idx.google.com/new?template=https%3A%2F%2Fgithub.com%2Fgoogle-gemini%2Fcookbook">
<picture>
<source
media="(prefers-color-scheme: dark)"
srcset="https://cdn.idx.dev/btn/try_light_32.svg">
<source
media="(prefers-color-scheme: light)"
srcset="https://cdn.idx.dev/btn/try_dark_32.svg">
<img
height="32"
alt="Try in IDX"
src="https://cdn.idx.dev/btn/try_purple_32.svg">
</picture>
</a>

To run any notebook in [IDX](https://idx.dev) replace the TODO in `.idx/dev.nix` with your Gemini API key by:
- Selecting "Add Gemini API" in the "Project IDX" panel in the sidebar
- Or by visiting https://g.co/ai/idxGetGeminiKey
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you want to track how many keys were created through IDX but I would prefer if we do not use different urls for the same website on the same page so I'd use https://aistudio.google.com/app/apikey


## Contributing
Contributions are welcome. See [contributing](https://github.com/google-gemini/cookbook/blob/main/CONTRIBUTING.md) to learn more.
Expand Down
7 changes: 7 additions & 0 deletions idx-template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "Gemini API Cookbook",
"description": "A collection of guides and examples for the Gemini API",
"categories": ["AI & ML"],
"icon": "https://www.gstatic.com/monospace/231128/logo_gemini_512.png",
"publisher": "Google LLC"
}
12 changes: 12 additions & 0 deletions idx-template.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{ pkgs, ... }: {
packages = [];
bootstrap = ''
mkdir "$out"
cp -rf ${./.}/* "$out"
mkdir "$out/.idx"
cp -rf ${./.}/.idx "$out"
rm "$out/idx-template.nix"
rm "$out/idx-template.json"
chmod -R u+w "$out"
'';
}
8 changes: 8 additions & 0 deletions requirements.txt
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would there be a way to move those files out of the main folder? Ideally I would prefer to have it as clean as possible and not risk a for a beginning user to run the requirements for nothing.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Dev environment
pip
autopep8

# App
google-generativeai # Client library for the Gemini API
Pillow # PIL image-loading library
ipywidgets # Jupyter Widgets
35 changes: 35 additions & 0 deletions scripts/api-key.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import os

def update_ipynb_files(directory):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you will also need to update all the internal links, otherwise the links in the "what's next" sections of each notebook will open the github instead of the corresponding notebook in IDX, breaking the learning path.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe that point will not be a problem as I'm trying to replace all the absolute links by relative ones.

"""
Updates all .ipynb files in the given directory and its subdirectories recursively,
replacing the specified text.

Args:
directory: The root directory to start the search.
"""
for root, _, files in os.walk(directory):
for filename in files:
if filename.endswith(".ipynb"):
filepath = os.path.join(root, filename)
with open(filepath, 'r') as f:
content = f.read()

old1_text = "from google.colab import userdata"
old2_text = "GOOGLE_API_KEY=userdata.get('GOOGLE_API_KEY')"
old3_text = r'userdata.get(\"GOOGLE_API_KEY\")'
old4_text = "userdata.get('GOOGLE_API_KEY')"
new1_text = "import os"
new2_text = "GOOGLE_API_KEY=os.environ['GOOGLE_API_KEY']"
new3_text = "os.environ['GOOGLE_API_KEY']"
updated_content = content.replace(old1_text, new1_text)
updated_content = updated_content.replace(old2_text, new2_text)
updated_content = updated_content.replace(old3_text, new3_text)
updated_content = updated_content.replace(old4_text, new3_text)

with open(filepath, 'w') as f:
f.write(updated_content)

if __name__ == "__main__":
directory = "."
update_ipynb_files(directory)