This is the code I took from the API docs in order to start multiturn chat
model = genai.GenerativeModel("gemini-pro")
messages = [{'role':'user', 'parts': ['hello']}]
response = model.generate_content(messages) # "Hello, how can I help"
messages.append(response.candidates[0].content)
messages.append({'role':'user', 'parts': ['How does quantum physics work?']})
response = model.generate_content(messages)
But I'm getting the following error:
TypeError: Could not create Blob
, expected Blob
, dict
or an Image
type(PIL.Image.Image
or IPython.display.Image
).
Got a: <class 'google.ai.generativelanguage_v1beta.types.content.Content'>
Value: parts {
text: "hello"
}
I also tried using the send_message feature, but I got the same error after I tried to use send_message the second time.
As a simple modification, how about the following modification?
genai.configure(api_key=apiKey)
model = genai.GenerativeModel("gemini-pro")
messages = [{"role": "user", "parts": ["hello"]}]
response = model.generate_content(messages)
messages.append({"role": "model", "parts": [response.text]})
print("--- response 1")
print(response.text)
messages.append({"role": "user", "parts": ["How does quantum physics work?"]})
response = model.generate_content(messages)
print("--- response 2")
print(response.text)
As another modification, how about the following modification? Ref
import google.generativeai as genai
apiKey = "###" # If you use API key, please set your API key.
genai.configure(api_key=apiKey)
model = genai.GenerativeModel("gemini-pro")
chat = model.start_chat()
def get_chat_response(chat: genai.ChatSession, prompt: str) -> str:
response = chat.send_message(prompt)
return response.text
prompt = "hello"
print(get_chat_response(chat, prompt))
prompt = "How does quantum physics work?"
print(get_chat_response(chat, prompt))