[Python] I am not getting the correct format to use gemini 1.0 pro latest with chat history

Discussão em 'Python' iniciado por Stack, Dezembro 22, 2024.

  1. Stack

    Stack Membro Participativo

    Can any one sugggest me the efficient and correct way?

    chat_history = [
    "question1",
    "response1",
    "question2",
    "response2"
    ]
    model_name : "gemini-1.0-pro-latest"
    system_prompt = "You are a helpful assistant."

    formatted_chat = [{'role': 'system', 'parts': [system_prompt]}]
    formatted_chat.extend([{'role': 'user', 'parts': [chat_history]} for i in range(0, len(chat_history), 2)])
    formatted_chat.extend([{'role': 'model', 'parts': [chat_history]} for i in range(1, len(chat_history), 2)])

    gen_model = genai.GenerativeModel(model_name=model)
    chat_model = gen_model.start_chat(history=formatted_chat)
    response = chat_model.send_message("what was my last question?").text

    print(response) # You'r last question was question2


    I tried to run the code. I am getting the response correct. but, i doubt, It's right way to do or not using python.

    messages = [{"role": "user", "parts": [system_prompt]},{"role": 'model', 'parts':['Sure.']}]
    messages.extend({"role": "user" if i % 2 == 0 else "model", "parts": [msg]} for i, msg in enumerate(chat_history))

    gen_model = genai.GenerativeModel(model_name=model)
    chat_model = gen_model.start_chat(history=messages)
    response = chat_model.send_message(content=question_prompt)

    Continue reading...

Compartilhe esta Página