mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-08 20:42:30 +08:00
refine OpenAi Api (#159)
This commit is contained in:
@ -29,7 +29,7 @@ def chunk(filename, binary, tenant_id, lang, callback=None, **kwargs):
|
||||
except Exception as e:
|
||||
callback(prog=-1, msg=str(e))
|
||||
return []
|
||||
img = Image.open(io.BytesIO(binary))
|
||||
img = Image.open(io.BytesIO(binary)).convert('RGB')
|
||||
doc = {
|
||||
"docnm_kwd": filename,
|
||||
"image": img
|
||||
|
||||
@ -43,8 +43,8 @@ class GptTurbo(Base):
|
||||
model=self.model_name,
|
||||
messages=history,
|
||||
**gen_conf)
|
||||
ans = response.output.choices[0]['message']['content'].strip()
|
||||
if response.output.choices[0].get("finish_reason", "") == "length":
|
||||
ans = response.choices[0].message.content.strip()
|
||||
if response.choices[0].finish_reason == "length":
|
||||
ans += "...\nFor the content length reason, it stopped, continue?" if is_english(
|
||||
[ans]) else "······\n由于长度的原因,回答被截断了,要继续吗?"
|
||||
return ans, response.usage.completion_tokens
|
||||
@ -114,12 +114,12 @@ class ZhipuChat(Base):
|
||||
history.insert(0, {"role": "system", "content": system})
|
||||
try:
|
||||
response = self.client.chat.completions.create(
|
||||
self.model_name,
|
||||
model=self.model_name,
|
||||
messages=history,
|
||||
**gen_conf
|
||||
)
|
||||
ans = response.output.choices[0]['message']['content'].strip()
|
||||
if response.output.choices[0].get("finish_reason", "") == "length":
|
||||
ans = response.choices[0].message.content.strip()
|
||||
if response.choices[0].finish_reason == "length":
|
||||
ans += "...\nFor the content length reason, it stopped, continue?" if is_english(
|
||||
[ans]) else "······\n由于长度的原因,回答被截断了,要继续吗?"
|
||||
return ans, response.usage.completion_tokens
|
||||
|
||||
@ -139,12 +139,16 @@ class ZhipuEmbed(Base):
|
||||
self.model_name = model_name
|
||||
|
||||
def encode(self, texts: list, batch_size=32):
|
||||
res = self.client.embeddings.create(input=texts,
|
||||
arr = []
|
||||
tks_num = 0
|
||||
for txt in texts:
|
||||
res = self.client.embeddings.create(input=txt,
|
||||
model=self.model_name)
|
||||
return np.array([d.embedding for d in res.data]
|
||||
), res.usage.total_tokens
|
||||
arr.append(res.data[0].embedding)
|
||||
tks_num += res.usage.total_tokens
|
||||
return np.array(arr), tks_num
|
||||
|
||||
def encode_queries(self, text):
|
||||
res = self.client.embeddings.create(input=text,
|
||||
model=self.model_name)
|
||||
return np.array(res["data"][0]["embedding"]), res.usage.total_tokens
|
||||
return np.array(res.data[0].embedding), res.usage.total_tokens
|
||||
|
||||
Reference in New Issue
Block a user