KevinHuSh
commited on
Commit
路
adb65d7
1
Parent(s):
f6d3d52
refine OpenAi Api (#159)
Browse files- README.md +1 -1
- api/apps/llm_app.py +1 -1
- rag/app/picture.py +1 -1
- rag/llm/chat_model.py +5 -5
- rag/llm/embedding_model.py +8 -4
README.md
CHANGED
|
@@ -127,7 +127,7 @@ Open your browser, enter the IP address of your server, _**Hallelujah**_ again!
|
|
| 127 |
# System Architecture Diagram
|
| 128 |
|
| 129 |
<div align="center" style="margin-top:20px;margin-bottom:20px;">
|
| 130 |
-
<img src="https://github.com/infiniflow/ragflow/assets/12318111/
|
| 131 |
</div>
|
| 132 |
|
| 133 |
# Configuration
|
|
|
|
| 127 |
# System Architecture Diagram
|
| 128 |
|
| 129 |
<div align="center" style="margin-top:20px;margin-bottom:20px;">
|
| 130 |
+
<img src="https://github.com/infiniflow/ragflow/assets/12318111/d6ac5664-c237-4200-a7c2-a4a00691b485" width="1000"/>
|
| 131 |
</div>
|
| 132 |
|
| 133 |
# Configuration
|
api/apps/llm_app.py
CHANGED
|
@@ -51,7 +51,7 @@ def set_api_key():
|
|
| 51 |
if len(arr[0]) == 0 or tc == 0:
|
| 52 |
raise Exception("Fail")
|
| 53 |
except Exception as e:
|
| 54 |
-
msg += f"\nFail to access embedding model({llm.llm_name}) using this api key."
|
| 55 |
elif not chat_passed and llm.model_type == LLMType.CHAT.value:
|
| 56 |
mdl = ChatModel[factory](
|
| 57 |
req["api_key"], llm.llm_name)
|
|
|
|
| 51 |
if len(arr[0]) == 0 or tc == 0:
|
| 52 |
raise Exception("Fail")
|
| 53 |
except Exception as e:
|
| 54 |
+
msg += f"\nFail to access embedding model({llm.llm_name}) using this api key." + str(e)
|
| 55 |
elif not chat_passed and llm.model_type == LLMType.CHAT.value:
|
| 56 |
mdl = ChatModel[factory](
|
| 57 |
req["api_key"], llm.llm_name)
|
rag/app/picture.py
CHANGED
|
@@ -29,7 +29,7 @@ def chunk(filename, binary, tenant_id, lang, callback=None, **kwargs):
|
|
| 29 |
except Exception as e:
|
| 30 |
callback(prog=-1, msg=str(e))
|
| 31 |
return []
|
| 32 |
-
img = Image.open(io.BytesIO(binary))
|
| 33 |
doc = {
|
| 34 |
"docnm_kwd": filename,
|
| 35 |
"image": img
|
|
|
|
| 29 |
except Exception as e:
|
| 30 |
callback(prog=-1, msg=str(e))
|
| 31 |
return []
|
| 32 |
+
img = Image.open(io.BytesIO(binary)).convert('RGB')
|
| 33 |
doc = {
|
| 34 |
"docnm_kwd": filename,
|
| 35 |
"image": img
|
rag/llm/chat_model.py
CHANGED
|
@@ -43,8 +43,8 @@ class GptTurbo(Base):
|
|
| 43 |
model=self.model_name,
|
| 44 |
messages=history,
|
| 45 |
**gen_conf)
|
| 46 |
-
ans = response.
|
| 47 |
-
if response.
|
| 48 |
ans += "...\nFor the content length reason, it stopped, continue?" if is_english(
|
| 49 |
[ans]) else "路路路路路路\n鐢变簬闀垮害鐨勫師鍥狅紝鍥炵瓟琚埅鏂簡锛岃缁х画鍚楋紵"
|
| 50 |
return ans, response.usage.completion_tokens
|
|
@@ -114,12 +114,12 @@ class ZhipuChat(Base):
|
|
| 114 |
history.insert(0, {"role": "system", "content": system})
|
| 115 |
try:
|
| 116 |
response = self.client.chat.completions.create(
|
| 117 |
-
self.model_name,
|
| 118 |
messages=history,
|
| 119 |
**gen_conf
|
| 120 |
)
|
| 121 |
-
ans = response.
|
| 122 |
-
if response.
|
| 123 |
ans += "...\nFor the content length reason, it stopped, continue?" if is_english(
|
| 124 |
[ans]) else "路路路路路路\n鐢变簬闀垮害鐨勫師鍥狅紝鍥炵瓟琚埅鏂簡锛岃缁х画鍚楋紵"
|
| 125 |
return ans, response.usage.completion_tokens
|
|
|
|
| 43 |
model=self.model_name,
|
| 44 |
messages=history,
|
| 45 |
**gen_conf)
|
| 46 |
+
ans = response.choices[0].message.content.strip()
|
| 47 |
+
if response.choices[0].finish_reason == "length":
|
| 48 |
ans += "...\nFor the content length reason, it stopped, continue?" if is_english(
|
| 49 |
[ans]) else "路路路路路路\n鐢变簬闀垮害鐨勫師鍥狅紝鍥炵瓟琚埅鏂簡锛岃缁х画鍚楋紵"
|
| 50 |
return ans, response.usage.completion_tokens
|
|
|
|
| 114 |
history.insert(0, {"role": "system", "content": system})
|
| 115 |
try:
|
| 116 |
response = self.client.chat.completions.create(
|
| 117 |
+
model=self.model_name,
|
| 118 |
messages=history,
|
| 119 |
**gen_conf
|
| 120 |
)
|
| 121 |
+
ans = response.choices[0].message.content.strip()
|
| 122 |
+
if response.choices[0].finish_reason == "length":
|
| 123 |
ans += "...\nFor the content length reason, it stopped, continue?" if is_english(
|
| 124 |
[ans]) else "路路路路路路\n鐢变簬闀垮害鐨勫師鍥狅紝鍥炵瓟琚埅鏂簡锛岃缁х画鍚楋紵"
|
| 125 |
return ans, response.usage.completion_tokens
|
rag/llm/embedding_model.py
CHANGED
|
@@ -139,12 +139,16 @@ class ZhipuEmbed(Base):
|
|
| 139 |
self.model_name = model_name
|
| 140 |
|
| 141 |
def encode(self, texts: list, batch_size=32):
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
| 143 |
model=self.model_name)
|
| 144 |
-
|
| 145 |
-
|
|
|
|
| 146 |
|
| 147 |
def encode_queries(self, text):
|
| 148 |
res = self.client.embeddings.create(input=text,
|
| 149 |
model=self.model_name)
|
| 150 |
-
return np.array(res
|
|
|
|
| 139 |
self.model_name = model_name
|
| 140 |
|
| 141 |
def encode(self, texts: list, batch_size=32):
|
| 142 |
+
arr = []
|
| 143 |
+
tks_num = 0
|
| 144 |
+
for txt in texts:
|
| 145 |
+
res = self.client.embeddings.create(input=txt,
|
| 146 |
model=self.model_name)
|
| 147 |
+
arr.append(res.data[0].embedding)
|
| 148 |
+
tks_num += res.usage.total_tokens
|
| 149 |
+
return np.array(arr), tks_num
|
| 150 |
|
| 151 |
def encode_queries(self, text):
|
| 152 |
res = self.client.embeddings.create(input=text,
|
| 153 |
model=self.model_name)
|
| 154 |
+
return np.array(res.data[0].embedding), res.usage.total_tokens
|