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
Transcript

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.

Open 05-system-instructions.ipynb from the starter project. Configure your API key in .env and then execute the first two code cells. When the execution completes successfully, add a third code cell. Now add the following code:

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.
'''

That’s quite a lot. Type this instruction carefully.

Note: To avoid any errors, add the instructions as a continuous chunk of text without pressing enter.

This code sets up some system instructions as a string to customize the model’s persona and the way it responds.

Execute the instructions you just added. On successful completion, add the following code:

model = genai.GenerativeModel(
  model_name = 'gemini-1.5-pro',
  system_instruction = instructions
)

You now use the Gemini 1.5 Pro model, since this newer model allows you to customize the system instructions. You pass the instructions into the generative AI model. Here, you could also pass in parameters for the configuration or safety settings. Execute this cell. Your model is now generated with a customized set of instructions.

Now, add:

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!')

This block of code starts a chat history and adds a prompt for the user input. You then added a while loop to continue the conversation until the user quits. You displayed the model’s responses nicely formatted. Execute this code cell, and give the following input at the top:

I put the keys on my desk. Press enter and wait for the model to respond.

Model responds. You’ll now notice the model has a new persona and it uses lots of sparkie emojis.

Then, to verify the model remembers your instruction, in the input prompt type:

Where did I put my keys?

Press enter. The model also remembers your previous conversation. You notice the model’s changed persona in its response.

You can see how it tries to accommodate the tone and requirements set forth in the system instructions.

Finally, type:

quit and then press enter. This ends the conversation.

You just created system instructions and customized the model to respond with text and emojis. You also gave a fresh personality to the model.

Now, you’ve finished your custom chatbot for text generation with the Gemini API. Great work!

See forum comments
Cinema mode Download course materials from Github
Previous: System Instructions Next: Conclusion