File size: 1,228 Bytes
00e272a
eb0f2cd
00e272a
7db09db
 
 
 
e5f609a
cf9bb9c
 
e5f609a
7db09db
 
 
 
 
 
2aa3040
cf9bb9c
e5f609a
7db09db
 
 
 
 
 
 
 
cf9bb9c
 
 
 
6859eec
7db09db
6859eec
5d730e7
cf9bb9c
6859eec
 
 
2aa3040
7db09db
cf9bb9c
 
 
 
 
7db09db
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import streamlit as st
from google import genai

st.title("Chat con Gemini")

# API key desde la interfaz
api_key = st.text_input("Gemini API Key", type="password")

if "history" not in st.session_state:
    st.session_state.history = []

# Mostrar historial
for message in st.session_state.history:
    role = "assistant" if message["role"] == "model" else "user"
    st.chat_message(role).write(message["parts"][0]["text"])

prompt = st.chat_input("Escribe tu mensaje")

if prompt:

    if not api_key:
        st.warning("Debes ingresar tu API key.")
        st.stop()

    # Crear cliente en cada ejecución
    client = genai.Client(api_key=api_key)

    # Agregar mensaje del usuario en formato correcto
    st.session_state.history.append({
        "role": "user",
        "parts": [{"text": prompt}]
    })

    # Crear chat con historial válido
    chat = client.chats.create(
        model="gemini-1.5-flash",
        history=st.session_state.history
    )

    response = chat.send_message(prompt)

    # Guardar respuesta del modelo correctamente
    st.session_state.history.append({
        "role": "model",
        "parts": [{"text": response.text}]
    })

    st.chat_message("assistant").write(response.text)