Text Generation with Google Gemini

Nov 14 2024 · Python 3.12, Google Gemini, JupyterLab, Visual Studio Code

Lesson 04: Building a Non-Streaming Chat App with Gemini

Demo: Chat History

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Demo

To start this demo, open the file from the starter project starting with the name 03-building. Open this file in Visual Studio Code. Add your API key to the .env file and execute the first code cell to install the libraries. Then, execute the second code cell to import the libraries and retrieve the API Key from the environment file.

model = genai.GenerativeModel('gemini-pro')
response = model.generate_content('I have two dogs and three cats.')
print(response.text)
You have a total of five pets.
print("="*50)
response = model.generate_content('How many paws are in my house?')
print(response.text)
chat_history = [
  "User: I have three cats and two dogs",
  "Model: That's great, I'll remember that",
  "User: How many paws are in my house?"
]
response = model.generate_content(chat_history)
print(response.text)
3 * 4 + 2 * 4 = 20
chat_history.append("Model: " + response.text)
chat_history.append("User: How many dogs do I have?")
response = model.generate_content(chat_history)
print(response.text)
model = genai.GenerativeModel('gemini-pro')
chat_session = model.start_chat()
chat_session.send_message("I have two dogs and three cats.")
response = chat_session.send_message("How many paws are in my house?")
print(response.text)
print(chat_session.history)
chat_session = model.start_chat(
  history=[
    {
      "role": "user",
      "parts": [
        "My name is Pinal and I am a Gemini",
      ],
    },
    {
      "role": "model",
      "parts": [
        "That's great to know, Pinal! Geminis are known for their
        curiosity, adaptability, and communication skills. \n\n
        Do you want to tell me more about yourself? I'd love to
        hear about your interests, hobbies, or anything else you'd
        like to share! 😊 \n",
      ],
    },
    {
      "role": "user",
      "parts": [
        "I worked at Kodeco for 25 years",
      ],
    },
    {
      "role": "model",
      "parts": [
        "Wow, 25 years at Kodeco! That's quite a commitment. I'm
        guessing you've seen a lot of changes over the years. \n\n
        Tell me, what's it like working there? Is it a good place
        to be? 😄 \n",
      ],
    },
  ]
)
response = chat_session.send_message("What's my name again?")
print(response.text)
print(chat_session.history[0].parts[0])
print(chat_session.history[-1].role.capitalize())
chat = model.start_chat(history=[])
prompt = input('User:')
while prompt != 'quit':
  response = chat.send_message(prompt)
  print(f'{chat.history[-1].role.capitalize()}: {chat.history[-1].parts[0]
    .text}')
  print('\n' + '-' * 100 + '\n')
  prompt = input('User:')
print('Ending Conversation ...')
time.sleep(2)
print('Talk to you next time!')
See forum comments
Cinema mode Download course materials from Github
Previous: Chat History Next: System Instructions