Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refine OpenAi Api #159

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ Open your browser, enter the IP address of your server, _**Hallelujah**_ again!
# System Architecture Diagram

<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>

# Configuration
Expand Down
2 changes: 1 addition & 1 deletion api/apps/llm_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def set_api_key():
if len(arr[0]) == 0 or tc == 0:
raise Exception("Fail")
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:
mdl = ChatModel[factory](
req["api_key"], llm.llm_name)
Expand Down
2 changes: 1 addition & 1 deletion rag/app/picture.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions rag/llm/chat_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def chat(self, system, history, gen_conf):
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
Expand Down Expand Up @@ -114,12 +114,12 @@ def chat(self, system, history, gen_conf):
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
Expand Down
12 changes: 8 additions & 4 deletions rag/llm/embedding_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,16 @@ def __init__(self, key, model_name="embedding-2"):
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