Skip to content

Instantly share code, notes, and snippets.

@richard-to
Created September 4, 2024 14:43
Show Gist options
  • Save richard-to/9a72fd7b02a9c3e5eff7d6d3394f911d to your computer and use it in GitHub Desktop.
Save richard-to/9a72fd7b02a9c3e5eff7d6d3394f911d to your computer and use it in GitHub Desktop.
Simple chat example using Gemini with Mesop.
import os
import google.generativeai as genai
import mesop as me
import mesop.labs as mel
genai.configure(api_key=os.getenv("GOOLGLE_API_KEY"))
SYSTEM_INSTRUCTION = "Talk like Shakespeare."
generation_config = genai.GenerationConfig(
temperature=1,
top_p=0.95,
top_k=64,
max_output_tokens=32768,
)
safety_settings = [
{
"category": "HARM_CATEGORY_HARASSMENT",
"threshold": "BLOCK_NONE",
},
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"threshold": "BLOCK_NONE",
},
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"threshold": "BLOCK_NONE",
},
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"threshold": "BLOCK_NONE",
},
]
gemini = genai.GenerativeModel(
model_name="gemini-1.5-flash",
system_instruction=SYSTEM_INSTRUCTION,
safety_settings=safety_settings,
generation_config=generation_config,
)
def on_load(e: me.LoadEvent):
me.set_theme_mode("system")
@me.page(
path="/simple-chat-gemini",
title="Mesop Chat - Gemini",
on_load=on_load,
)
def page():
mel.chat(transform, title="Mesop Chat - Gemini", bot_user="Gemini Bot")
def transform(input: str, history: list[mel.ChatMessage]):
for response in gemini.generate_content(input, stream=True):
yield response.text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment