Gemini

Notes of the course Pair Programming with a LLM from DeepLearning.AI. Check it out here

Things needed

  • An API key, you can get one here

  • Generative AI Libraries

  • Basic python skills

from utils import get_api_key
import os
import google.generativeai as genai
from google.api_core import client_options as client_options_lib

genai.configure(
    api_key=get_api_key(),
    transport="rest",
    client_options=client_options_lib.ClientOptions(
        api_endpoint=os.getenv("GOOGLE_API_BASE"),
    )
)
# Explore the available models
for m in genai.list_models():
    print(f"name: {m.name}")
    print(f"description: {m.description}")
    print(f"generation methods:{m.supported_generation_methods}\n")
models = [m for m in genai.list_models() 
          if 'generateText' 
          in m.supported_generation_methods]
models
# Set the model to connect to the Gemini API
model_flash = genai.GenerativeModel(model_name='gemini-1.5-flash')
# Helper with Gemini API
def generate_text(prompt,
                  model=model_flash,
                  temperature=0.0):
    return model_flash.generate_content(prompt,
                                  generation_config={'temperature':temperature})
# Ask the LLM how to write some code
prompt = "Show me how to iterate across a list in Python."
# Generate the text
completion = generate_text(prompt)
print(completion.text)
# Ask it to write code right away instead of giving code along with explainations
prompt = "write code to iterate across a list in Python"
completion = generate_text(prompt)
print(completion.text)

Using a String Template

Pair programming scenarios

Improve existing code

Simplify code

Write test cases

Make code more efficient

Debug your code

Last updated