下面是向 Gemini 发送提示和 pdf 文件的片段。
def runGemini(prompt, model, document=None):
model = genai.GenerativeModel(model)
content = prompt
if document:
content = [
document,
"What is this document?",
]
response = model.generate_content(
content,
)
return response.text
这里我生成 PDF 的内容部分:
import base64
bytes = pdf_file.getvalue()
base64_bytes = base64.b64encode(bytes).decode("utf-8")
blob = glm.Blob(mime_type="application/pdf", data=base64_bytes)
document = glm.Part(inline_data=blob)
首先,我很难重新创建它,因为我试图猜测您创建的 pdf 对象是什么以及如何创建。
我最终这样做了
为了创建一个文档,我已经这样做了。我看不到你在做什么。但它确实重现了问题。
def get_document(pdf_file):
bytes = pdf_file.getvalue()
base64_bytes = base64.b64encode(bytes).decode("utf-8")
blob = glm.Blob(mime_type="application/pdf", data=base64_bytes)
return glm.Part(inline_data=blob)
if __name__ == "__main__":
pdf_data = b"PDF file content here"
pdf_file = BytesIO(pdf_data)
document = get_document(pdf_file)
print(document)
当我打印返回的文档对象时,我得到了。
inline_data {
mime_type: "application/pdf"
data: "PDF file content here"
}
失败的原因是 mime 类型不是“application/pdf”而是“text/basic”
如果您尝试将 pdf 文件的字节数组直接加载到提示中,据我所知,这是行不通的,您需要加载 pdf 的文本,然后将其作为“文本/基本”发送