我对法学硕士的开发还很陌生。 我正在尝试使用 google 嵌入在 Streamlit 中使用 Gemini 的 API 编写一个基于 RAG 的应用程序。 该应用程序的目标是在回答用户的请求时从 PDF(这是我的 RAG 的知识库)中检索信息。 我使用 Streamlit 进行接口,使用 FAISS 进行矢量 DB。
这是我拨打电话的代码:
# Get the user's question
question = st.chat_input("Ask here")
# Handle the user's question
if question:
vectordb = st.session_state.get("vectordb", None)
if not vectordb:
with st.message("assistant"):
st.write("You need to provide a PDF")
st.stop()
# Search the vectordb for similar content to the user's question
search_results = vectordb.similarity_search(question, k=3)
# sources of result
pdf_extract = "/n ".join([result.page_content for result in search_results])
# Update the prompt with the pdf extract
prompt[0] = {
"role": "system",
"content": prompt_template.format(pdf_extract=pdf_extract),
}
# Add the user's question to the prompt and display it
prompt.append({"role": "user", "content": question})
with st.chat_message("user"):
st.write(question)
# Display an empty assistant message while waiting for the response
with st.chat_message("assistant"):
botmsg = st.empty()
response = []
result = ""
response = model.generate_content(prompt)
botmsg.write(result)
型号是
model = genai.GenerativeModel('gemini-pro')
设置 API 密钥等后。 我没有包含这部分代码,因为我认为与该问题无关。
当我执行脚本时,我遇到此错误:
KeyError: "Could not recognize the intended type of the `dict`. A `Content` should have a 'parts' key. A `Part` should have a 'inline_data' or a 'text' key. A `Blob` should have 'mime_type' and 'data' keys. Got keys: ['role', 'content']"
我之前的代码使用了 openAI 的 API 和 OpenAI Embedding 模型,并且它有效:
for chunk in st.session_state.chat.send_message(prompt):
text = chunk.choices[0].get("delta", {}).get("content")
if text is not None:
response.append(text)
result = "".join(response).strip()
botmsg.write(result)
Gemini 对应的代码是这样的:(出现在前面的代码片段中)
response = model.generate_content(prompt)
botmsg.write(result)
我该如何解决这个问题?我不明白这个错误,我很确定代码中缺少一些东西。 如果有人遇到过这种情况,或者知道为什么会发生这种情况,我将不胜感激。
提前谢谢您。
这是一个帮助您入门的基本测试脚本。希望这有帮助:)
from vertexai.preview.generative_models import GenerativeModel, GenerationConfig, Part, Content, HarmCategory, HarmBlockThreshold, ResponseBlockedError
class Gemini:
def __init__(self, streaming: bool = False):
# Load the model
self.model = GenerativeModel("gemini-pro")
self.streaming = streaming
# Generation config
self.config = GenerationConfig(
temperature=0.0,
top_p=0.8,
top_k=32,
candidate_count=1,
max_output_tokens=2048,
)
# Safety config
self.safety_config = {
HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_ONLY_HIGH,
HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_ONLY_HIGH,
HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_ONLY_HIGH,
HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_ONLY_HIGH,
HarmCategory.HARM_CATEGORY_UNSPECIFIED: HarmBlockThreshold.BLOCK_ONLY_HIGH,
}
def invoke(self, prompt: dict):
# Format the user input as a Content object
user_input = Content(
role="user",
parts=[
Part.from_text(prompt["input"]),
]
)
response = self.model.generate_content(
contents=user_input,
generation_config=self.config,
safety_settings=self.safety_config,
stream=self.streaming,
tools=[]
)
return response
# Initialise the model
model = Gemini(streaming=False)
# Test the model
generated_response = model.invoke({"input": "ELI5 in 50 words or less: Why is the sky blue?"})
# Print the response with basic exception handling
try:
if hasattr(generated_response.candidates[0].content.parts[0], 'text'):
print(generated_response.candidates[0].content.parts[0].text)
except ValueError as e:
print("An error occured: ", e)