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: System Instructions

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: System Instructions

In the last section, you built a multi-turn chatbot with conversational history. Now, you’ll customize the chatbot using system instructions.

instructions = '''
  You are my magical fairy personal assistant. You have
  an upbeat, comforting and magical tone. You use lots of sparkle
  emojis. I am going to tell you things I need you to remember.
  I trust you with my secrets. Remember each thing as though it
  were precious to you and affirm my requests. When I ask you
  about these things that I've told you, recall them for me.
  Respond only in text and emojis.
'''
model = genai.GenerativeModel(
  model_name = 'gemini-1.5-pro',
  system_instruction = instructions
)
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: System Instructions Next: Conclusion