Skip to content
Snippets Groups Projects
Unverified Commit bf2e3d7f authored by KevinHuSh's avatar KevinHuSh Committed by GitHub
Browse files

refine OpenAi Api (#159)

parent 0cb95c68
No related branches found
No related tags found
No related merge requests found
...@@ -127,7 +127,7 @@ Open your browser, enter the IP address of your server, _**Hallelujah**_ again! ...@@ -127,7 +127,7 @@ Open your browser, enter the IP address of your server, _**Hallelujah**_ again!
# System Architecture Diagram # System Architecture Diagram
<div align="center" style="margin-top:20px;margin-bottom:20px;"> <div align="center" style="margin-top:20px;margin-bottom:20px;">
<img src="https://github.com/infiniflow/ragflow/assets/12318111/39c8e546-51ca-4b50-a1da-83731b540cd0" width="1000"/> <img src="https://github.com/infiniflow/ragflow/assets/12318111/d6ac5664-c237-4200-a7c2-a4a00691b485" width="1000"/>
</div> </div>
# Configuration # Configuration
......
...@@ -51,7 +51,7 @@ def set_api_key(): ...@@ -51,7 +51,7 @@ def set_api_key():
if len(arr[0]) == 0 or tc == 0: if len(arr[0]) == 0 or tc == 0:
raise Exception("Fail") raise Exception("Fail")
except Exception as e: except Exception as e:
msg += f"\nFail to access embedding model({llm.llm_name}) using this api key." msg += f"\nFail to access embedding model({llm.llm_name}) using this api key." + str(e)
elif not chat_passed and llm.model_type == LLMType.CHAT.value: elif not chat_passed and llm.model_type == LLMType.CHAT.value:
mdl = ChatModel[factory]( mdl = ChatModel[factory](
req["api_key"], llm.llm_name) req["api_key"], llm.llm_name)
......
...@@ -29,7 +29,7 @@ def chunk(filename, binary, tenant_id, lang, callback=None, **kwargs): ...@@ -29,7 +29,7 @@ def chunk(filename, binary, tenant_id, lang, callback=None, **kwargs):
except Exception as e: except Exception as e:
callback(prog=-1, msg=str(e)) callback(prog=-1, msg=str(e))
return [] return []
img = Image.open(io.BytesIO(binary)) img = Image.open(io.BytesIO(binary)).convert('RGB')
doc = { doc = {
"docnm_kwd": filename, "docnm_kwd": filename,
"image": img "image": img
......
...@@ -43,8 +43,8 @@ class GptTurbo(Base): ...@@ -43,8 +43,8 @@ class GptTurbo(Base):
model=self.model_name, model=self.model_name,
messages=history, messages=history,
**gen_conf) **gen_conf)
ans = response.output.choices[0]['message']['content'].strip() ans = response.choices[0].message.content.strip()
if response.output.choices[0].get("finish_reason", "") == "length": if response.choices[0].finish_reason == "length":
ans += "...\nFor the content length reason, it stopped, continue?" if is_english( ans += "...\nFor the content length reason, it stopped, continue?" if is_english(
[ans]) else "······\n由于长度的原因,回答被截断了,要继续吗?" [ans]) else "······\n由于长度的原因,回答被截断了,要继续吗?"
return ans, response.usage.completion_tokens return ans, response.usage.completion_tokens
...@@ -114,12 +114,12 @@ class ZhipuChat(Base): ...@@ -114,12 +114,12 @@ class ZhipuChat(Base):
history.insert(0, {"role": "system", "content": system}) history.insert(0, {"role": "system", "content": system})
try: try:
response = self.client.chat.completions.create( response = self.client.chat.completions.create(
self.model_name, model=self.model_name,
messages=history, messages=history,
**gen_conf **gen_conf
) )
ans = response.output.choices[0]['message']['content'].strip() ans = response.choices[0].message.content.strip()
if response.output.choices[0].get("finish_reason", "") == "length": if response.choices[0].finish_reason == "length":
ans += "...\nFor the content length reason, it stopped, continue?" if is_english( ans += "...\nFor the content length reason, it stopped, continue?" if is_english(
[ans]) else "······\n由于长度的原因,回答被截断了,要继续吗?" [ans]) else "······\n由于长度的原因,回答被截断了,要继续吗?"
return ans, response.usage.completion_tokens return ans, response.usage.completion_tokens
......
...@@ -139,12 +139,16 @@ class ZhipuEmbed(Base): ...@@ -139,12 +139,16 @@ class ZhipuEmbed(Base):
self.model_name = model_name self.model_name = model_name
def encode(self, texts: list, batch_size=32): 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) model=self.model_name)
return np.array([d.embedding for d in res.data] arr.append(res.data[0].embedding)
), res.usage.total_tokens tks_num += res.usage.total_tokens
return np.array(arr), tks_num
def encode_queries(self, text): def encode_queries(self, text):
res = self.client.embeddings.create(input=text, res = self.client.embeddings.create(input=text,
model=self.model_name) 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment