问题验证
- 我已在文档和Discord中寻找答案。
问题
# This class will transform video to text and images
class VideoProcessor:
def __init__(self, video_path, output_audio_path, output_folder):
self.video_path = video_path
self.output_audio_path = output_audio_path
self.image_path = output_folder
self.text_path = output_folder
def extract_audio(self):
video = VideoFileClip(os.path.join(self.video_path, "input.mp4"))
audio_part = video.audio
audio_part.write_audiofile(os.path.join(self.output_audio_path, "output_audio.mp3"))
def extract_text(self):
model = whisper.load_model("base")
result = model.transcribe(os.path.join(self.output_audio_path, "output_audio.mp3"))
with open(os.path.join(self.text_path, "audio.md"), "w") as file:
file.write(result['text'])
file.close()
def extract_frames(self):
clip = VideoFileClip(os.path.join(self.video_path, "input.mp4"))
clip.write_images_sequence(os.path.join(self.image_path, "frame%04d.png"), fps=0.2)
def process_video(self):
self.extract_audio()
self.extract_text()
self.extract_frames()
# This class maker sure RAG and ask
class retriever_ask:
def __init__(self, output_folder, similarity_top_k, image_similarity_top_k) -> None:
self.text_store = LanceDBVectorStore(uri="lancedb", table_name="text_collection")
self.image_store = LanceDBVectorStore(uri="lancedb", table_name="image_collection")
self.output_folder = output_folder
self.similarity_top_k = similarity_top_k
self.image_similarity_top_k = image_similarity_top_k
# self.llm = Ollama(model='llava', request_timeout=100)
self.llm = OllamaMultiModal(model='llava', request_timeout=3600)
self.qa_tmpl_str = (
"""
Given the provided information, including relevant images and retrieved context from the video,\
accurately and precisely answer the query without any additional prior knowledge.\n"
"Please ensure honesty and responsibility, refraining from any racist or sexist remarks.\n"
"---------------------\n"
"Context: {context_str}\n"
"Metadata for video: {metadata_str} \n"
"---------------------\n"
"Query: {query_str}\n"
"Answer: "
"""
)
def retriever_engine(self):
documents = SimpleDirectoryReader(self.output_folder).load_data()
storage_context = StorageContext.from_defaults(vector_store=self.text_store, image_store=self.image_store)
index = MultiModalVectorStoreIndex.from_documents(documents, storage_context=storage_context)
retriever_engine = index.as_retriever(similarity_top_k=self.similarity_top_k, image_similarity_top_k=self.image_similarity_top_k)
return retriever_engine
def retrieve(self, retriever_engine, query_str):
retrieval_results = retriever_engine.retrieve(query_str)
retrieved_image = []
retrieved_text = []
for res_node in retrieval_results:
if isinstance(res_node.node, ImageNode):
retrieved_image.append(res_node.node.metadata["file_path"])
else:
retrieved_text.append(res_node.text)
return retrieved_image, retrieved_text
def ask(self):
retriever_engine = self.retriever_engine()
while True:
try:
query_str = input('\033[94m' +"Prompt: " + '\033[0m')
image, text = self.retrieve(retriever_engine=retriever_engine, query_str=query_str)
context_str = "".join(text)
metadata_str= image
image_documents = SimpleDirectoryReader(input_dir=self.output_folder, input_files=image).load_data()
response = self.llm.complete(prompt=self.qa_tmpl_str.format(context_str=context_str, query_str=query_str, metadata_str=metadata_str),image_documents=image_documents)
print(response)
except KeyboardInterrupt:
break
if __name__ == '__main__':
Settings.embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5")
video_path = './video/'
output_audio_path = './audio/'
output_folder= './output/'
# process video to images and text
processor = VideoProcessor(video_path, output_audio_path, output_folder)
processor.process_video()
# Ask question
engine = retriever_ask(output_folder, 5, 5)
engine.ask()
但我遇到了以下问题:
Prompt: hello
Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/httpx/_transports/default.py", line 69, in map_httpcore_exceptions
yield
File "/usr/local/lib/python3.8/dist-packages/httpx/_transports/default.py", line 233, in handle_request
resp = self._pool.handle_request(req)
File "/usr/local/lib/python3.8/dist-packages/httpcore/_sync/connection_pool.py", line 216, in handle_request
raise exc from None
File "/usr/local/lib/python3.8/dist-packages/httpcore/_sync/connection_pool.py", line 196, in handle_request
response = connection.handle_request(
File "/usr/local/lib/python3.8/dist-packages/httpcore/_sync/connection.py", line 101, in handle_request
return self._connection.handle_request(request)
File "/usr/local/lib/python3.8/dist-packages/httpcore/_sync/http11.py", line 143, in handle_request
raise exc
File "/usr/local/lib/python3.8/dist-packages/httpcore/_sync/http11.py", line 113, in handle_request
) = self._receive_response_headers(**kwargs)
File "/usr/local/lib/python3.8/dist-packages/httpcore/_sync/http11.py", line 186, in _receive_response_headers
event = self._receive_event(timeout=timeout)
File "/usr/local/lib/python3.8/dist-packages/httpcore/_sync/http11.py", line 224, in _receive_event
data = self._network_stream.read(
File "/usr/local/lib/python3.8/dist-packages/httpcore/_backends/sync.py", line 126, in read
return self._sock.recv(max_bytes)
File "/usr/lib/python3.8/contextlib.py", line 131, in __exit__
self.gen.throw(type, value, traceback)
File "/usr/local/lib/python3.8/dist-packages/httpcore/_exceptions.py", line 14, in map_exceptions
raise to_exc(exc) from exc
httpcore.ReadTimeout: timed out
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "./mulitRAG.py", line 153, in <module>
engine.ask()
File "./mulitRAG.py", line 135, in ask
response = self.llm.complete(prompt=self.qa_tmpl_str.format(context_str=context_str, query_str=query_str, metadata_str=metadata_str),image_documents=image_documents)
File "/usr/local/lib/python3.8/dist-packages/llama_index/multi_modal_llms/ollama/base.py", line 160, in complete
response = self._client.generate(
File "/usr/local/lib/python3.8/dist-packages/ollama/_client.py", line 126, in generate
return self._request_stream(
File "/usr/local/lib/python3.8/dist-packages/ollama/_client.py", line 97, in _request_stream
return self._stream(*args, **kwargs) if stream else self._request(*args, **kwargs).json()
File "/usr/local/lib/python3.8/dist-packages/ollama/_client.py", line 68, in _request
response = self._client.request(method, url, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/httpx/_client.py", line 827, in request
return self.send(request, auth=auth, follow_redirects=follow_redirects)
File "/usr/local/lib/python3.8/dist-packages/httpx/_client.py", line 914, in send
response = self._send_handling_auth(
File "/usr/local/lib/python3.8/dist-packages/httpx/_client.py", line 942, in _send_handling_auth
response = self._send_handling_redirects(
File "/usr/local/lib/python3.8/dist-packages/httpx/_client.py", line 979, in _send_handling_redirects
response = self._send_single_request(request)
File "/usr/local/lib/python3.8/dist-packages/httpx/_client.py", line 1015, in _send_single_request
response = transport.handle_request(request)
File "/usr/local/lib/python3.8/dist-packages/httpx/_transports/default.py", line 233, in handle_request
resp = self._pool.handle_request(req)
File "/usr/lib/python3.8/contextlib.py", line 131, in __exit__
self.gen.throw(type, value, traceback)
File "/usr/local/lib/python3.8/dist-packages/httpx/_transports/default.py", line 86, in map_httpcore_exceptions
raise mapped_exc(message) from exc
httpx.ReadTimeout: timed out
我确信我的电脑正在对我的问题进行推理,但我没有得到对问题的回应。
7条答案
按热度按时间qqrboqgw1#
要解决在运行本地多模态RAG聊天机器人代码时遇到的超时问题,你可以在
OllamaMultiModal
类中设置request_timeout
参数。此参数指定向 Ollama API 服务器发出 HTTP 请求的超时时间。以下是如何设置
request_timeout
参数的方法:在你的代码中,你已经将
request_timeout
设置为 3600 秒,这对于大多数情况来说应该足够了。然而,如果你仍然遇到超时问题,可以考虑以下附加步骤:request_timeout
值以容纳更长的处理时间。通过遵循这些步骤,你应该能够诊断并解决多模态 RAG 聊天机器人代码中的超时问题。
tpgth1q72#
所以有人帮忙,我是不是用了一些错误的功能?
tag5nh1u3#
要解决本地多模态RAG聊天机器人代码中的超时问题,请确保将
request_timeout
参数正确传递给Client
初始化。提供的代码已经使用request_timeout
值在Client
初始化中设置了timeout
参数。以下是相关代码部分:
如果您仍然遇到超时问题,请考虑检查以下内容:
OllamaMultiModal
示例时,请仔细检查request_timeout
是否正确设置。此外,请确保
ollama
库已正确安装和导入,如OllamaMultiModal
类所示:如果所有配置都正确,问题仍然存在,您可能需要进一步调查服务器日志或网络条件,以确定超时的根源。
此外,请确保
httpx.Client
与Timeout
对象正确使用,如Ollama
类所示(来自llama-index
库):请确保正确示例化并将
Timeout
对象传递给httpx.Client
以正确处理请求超时。要继续与 Dosu 交谈,请提及 @dosu。9njqaruj4#
你好,我想使用 clip 来嵌入我的图片和文本,我该如何使用它?你能给我一个例子吗?
isr3a4wc5#
要切换到使用CLIP将您的图像和文本嵌入到本地多模态RAG聊天机器人中,您可以使用
llama_index/embeddings/clip/base.py
文件中的ClipEmbedding
类。以下是逐步指南:ClipEmbedding
类:以下是一个完整示例:
此代码将初始化CLIP模型,为提供的文本和图像生成嵌入,并打印嵌入1(https://github.com/run-llama/llama_index/blob/main/llama-index-legacy/llama_index/legacy/embeddings/clip.py)2(https://github.com/run-llama/llama_index/blob/main/llama-index-integrations/embeddings/llama-index-embeddings-clip/llama_index/embeddings/clip/base.py)。
arknldoa6#
要切换到使用CLIP将您的图像和文本嵌入到本地多模态RAG聊天机器人中,您可以使用
llama_index/embeddings/clip/base.py
文件中的ClipEmbedding
类。以下是逐步指南:ClipEmbedding
类:以下是一个完整的例子:
这段代码将初始化CLIP模型,为提供的文本和图像生成嵌入,并打印嵌入 1(https://github.com/run-llama/llama_index/blob/main/llama-index-legacy/llama_index/legacy/embeddings/clip.py) 和 2(https://github.com/run-llama/llama_index/blob/main/llama-index-integrations/embeddings/llama-index-embeddings-clip/llama_index/embeddings/clip/base.py) 。
使用clip与LanceDBVectorStore的示例
w8f9ii697#
将当前的嵌入模型替换为CLIP并在本地多模态RAG聊天机器人中使用它,请按照以下步骤操作:
ClipEmbedding
类:使用提供的
ClipEmbedding
类,使用CLIP模型生成嵌入。这里是完整的类定义:LanceDBVectorStore
集成:假设你有一个
LanceDBVectorStore
类,你需要将其与ClipEmbedding
类集成。下面是一个简化的示例,展示了如何进行此操作:这段代码演示了如何初始化
ClipEmbedding
类,生成文本和图像嵌入,并将它们添加到LanceDBVectorStore
中。根据你对LanceDBVectorStore
1(https://github.com/run-llama/llama_index/blob/main/llama-index-legacy/llama_index/legacy/embeddings/clip.py)2(https://github.com/run-llama/llama_index/blob/main/llama-index-integrations/embeddings/llama-index-embeddings-clip/llama_index/embeddings/clip/base.py)3(https://github.com/run-llama/llama_index/blob/main/llama-index-integrations/embeddings/llama-index-embeddings-clip/llama_index/embeddings/clip/init.py) 的具体实现,调整集成方式。