mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-08 20:42:30 +08:00
### What problem does this PR solve? Fix: can't upload image in ollama model #10447 ### Type of change - [X] Bug Fix (non-breaking change which fixes an issue) ### Change all `image=[]` to `image = None` Changing `image=[]` to `images=None` avoids Python’s mutable default parameter issue. If you keep `images=[]`, all calls share the same list, so modifying it (e.g., images.append()) will affect later calls. Using images=None and creating a new list inside the function ensures each call is independent. This change does not affect current behavior — it simply makes the code safer and more predictable. 把 `images=[]` 改成 `images=None` 是为了避免 Python 默认参数的可变对象问题。 如果保留 `images=[]`,所有调用都会共用同一个列表,一旦修改就会影响后续调用。 改成 None 并在函数内部重新创建列表,可以确保每次调用都是独立的。 这个修改不会影响现有运行结果,只是让代码更安全、更可控。
This commit is contained in:
@ -50,7 +50,7 @@ class Base(ABC):
|
||||
def describe_with_prompt(self, image, prompt=None):
|
||||
raise NotImplementedError("Please implement encode method!")
|
||||
|
||||
def _form_history(self, system, history, images=[]):
|
||||
def _form_history(self, system, history, images=None):
|
||||
hist = []
|
||||
if system:
|
||||
hist.append({"role": "system", "content": system})
|
||||
@ -78,7 +78,7 @@ class Base(ABC):
|
||||
})
|
||||
return pmpt
|
||||
|
||||
def chat(self, system, history, gen_conf, images=[], **kwargs):
|
||||
def chat(self, system, history, gen_conf, images=None, **kwargs):
|
||||
try:
|
||||
response = self.client.chat.completions.create(
|
||||
model=self.model_name,
|
||||
@ -89,7 +89,7 @@ class Base(ABC):
|
||||
except Exception as e:
|
||||
return "**ERROR**: " + str(e), 0
|
||||
|
||||
def chat_streamly(self, system, history, gen_conf, images=[], **kwargs):
|
||||
def chat_streamly(self, system, history, gen_conf, images=None, **kwargs):
|
||||
ans = ""
|
||||
tk_count = 0
|
||||
try:
|
||||
@ -228,7 +228,7 @@ class QWenCV(GptV4):
|
||||
base_url = "https://dashscope.aliyuncs.com/compatible-mode/v1"
|
||||
super().__init__(key, model_name, lang=lang, base_url=base_url, **kwargs)
|
||||
|
||||
def chat(self, system, history, gen_conf, images=[], video_bytes=None, filename=""):
|
||||
def chat(self, system, history, gen_conf, images=None, video_bytes=None, filename=""):
|
||||
if video_bytes:
|
||||
try:
|
||||
summary, summary_num_tokens = self._process_video(video_bytes, filename)
|
||||
@ -506,7 +506,7 @@ class OllamaCV(Base):
|
||||
options["frequency_penalty"] = gen_conf["frequency_penalty"]
|
||||
return options
|
||||
|
||||
def _form_history(self, system, history, images=[]):
|
||||
def _form_history(self, system, history, images=None):
|
||||
hist = deepcopy(history)
|
||||
if system and hist[0]["role"] == "user":
|
||||
hist.insert(0, {"role": "system", "content": system})
|
||||
@ -547,7 +547,7 @@ class OllamaCV(Base):
|
||||
except Exception as e:
|
||||
return "**ERROR**: " + str(e), 0
|
||||
|
||||
def chat(self, system, history, gen_conf, images=[]):
|
||||
def chat(self, system, history, gen_conf, images=None):
|
||||
try:
|
||||
response = self.client.chat(
|
||||
model=self.model_name,
|
||||
@ -561,7 +561,7 @@ class OllamaCV(Base):
|
||||
except Exception as e:
|
||||
return "**ERROR**: " + str(e), 0
|
||||
|
||||
def chat_streamly(self, system, history, gen_conf, images=[]):
|
||||
def chat_streamly(self, system, history, gen_conf, images=None):
|
||||
ans = ""
|
||||
try:
|
||||
response = self.client.chat(
|
||||
@ -596,7 +596,7 @@ class GeminiCV(Base):
|
||||
self.lang = lang
|
||||
Base.__init__(self, **kwargs)
|
||||
|
||||
def _form_history(self, system, history, images=[]):
|
||||
def _form_history(self, system, history, images=None):
|
||||
hist = []
|
||||
if system:
|
||||
hist.append({"role": "user", "parts": [system, history[0]["content"]]})
|
||||
@ -633,7 +633,7 @@ class GeminiCV(Base):
|
||||
return res.text, total_token_count_from_response(res)
|
||||
|
||||
|
||||
def chat(self, system, history, gen_conf, images=[], video_bytes=None, filename=""):
|
||||
def chat(self, system, history, gen_conf, images=None, video_bytes=None, filename=""):
|
||||
if video_bytes:
|
||||
try:
|
||||
summary, summary_num_tokens = self._process_video(video_bytes, filename)
|
||||
@ -651,7 +651,7 @@ class GeminiCV(Base):
|
||||
except Exception as e:
|
||||
return "**ERROR**: " + str(e), 0
|
||||
|
||||
def chat_streamly(self, system, history, gen_conf, images=[]):
|
||||
def chat_streamly(self, system, history, gen_conf, images=None):
|
||||
ans = ""
|
||||
response = None
|
||||
try:
|
||||
@ -782,7 +782,7 @@ class NvidiaCV(Base):
|
||||
total_token_count_from_response(response)
|
||||
)
|
||||
|
||||
def chat(self, system, history, gen_conf, images=[], **kwargs):
|
||||
def chat(self, system, history, gen_conf, images=None, **kwargs):
|
||||
try:
|
||||
response = self._request(self._form_history(system, history, images), gen_conf)
|
||||
return (
|
||||
@ -792,7 +792,7 @@ class NvidiaCV(Base):
|
||||
except Exception as e:
|
||||
return "**ERROR**: " + str(e), 0
|
||||
|
||||
def chat_streamly(self, system, history, gen_conf, images=[], **kwargs):
|
||||
def chat_streamly(self, system, history, gen_conf, images=None, **kwargs):
|
||||
total_tokens = 0
|
||||
try:
|
||||
response = self._request(self._form_history(system, history, images), gen_conf)
|
||||
@ -858,7 +858,7 @@ class AnthropicCV(Base):
|
||||
gen_conf["max_tokens"] = self.max_tokens
|
||||
return gen_conf
|
||||
|
||||
def chat(self, system, history, gen_conf, images=[]):
|
||||
def chat(self, system, history, gen_conf, images=None):
|
||||
gen_conf = self._clean_conf(gen_conf)
|
||||
ans = ""
|
||||
try:
|
||||
@ -879,7 +879,7 @@ class AnthropicCV(Base):
|
||||
except Exception as e:
|
||||
return ans + "\n**ERROR**: " + str(e), 0
|
||||
|
||||
def chat_streamly(self, system, history, gen_conf, images=[]):
|
||||
def chat_streamly(self, system, history, gen_conf, images=None):
|
||||
gen_conf = self._clean_conf(gen_conf)
|
||||
total_tokens = 0
|
||||
try:
|
||||
@ -963,13 +963,13 @@ class GoogleCV(AnthropicCV, GeminiCV):
|
||||
else:
|
||||
return GeminiCV.describe_with_prompt(self, image, prompt)
|
||||
|
||||
def chat(self, system, history, gen_conf, images=[]):
|
||||
def chat(self, system, history, gen_conf, images=None):
|
||||
if "claude" in self.model_name:
|
||||
return AnthropicCV.chat(self, system, history, gen_conf, images)
|
||||
else:
|
||||
return GeminiCV.chat(self, system, history, gen_conf, images)
|
||||
|
||||
def chat_streamly(self, system, history, gen_conf, images=[]):
|
||||
def chat_streamly(self, system, history, gen_conf, images=None):
|
||||
if "claude" in self.model_name:
|
||||
for ans in AnthropicCV.chat_streamly(self, system, history, gen_conf, images):
|
||||
yield ans
|
||||
|
||||
Reference in New Issue
Block a user