forked from infiniflow/ragflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
### What problem does this PR solve? Includes SDK for creating, updating sessions, getting sessions, listing sessions, and dialogues infiniflow#1102 ### Type of change - [x] New Feature (non-breaking change which adds functionality) --------- Co-authored-by: liuhua <[email protected]>
- Loading branch information
Showing
7 changed files
with
298 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 86 additions & 71 deletions
157
sdk/python/ragflow/modules/chat_assistant.py → sdk/python/ragflow/modules/assistant.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,86 @@ | ||
from typing import List | ||
|
||
from .base import Base | ||
from .session import Session, Message | ||
|
||
|
||
class Assistant(Base): | ||
def __init__(self, rag, res_dict): | ||
self.id = "" | ||
self.name = "assistant" | ||
self.avatar = "path/to/avatar" | ||
self.knowledgebases = ["kb1"] | ||
self.llm = Assistant.LLM(rag, {}) | ||
self.prompt = Assistant.Prompt(rag, {}) | ||
super().__init__(rag, res_dict) | ||
|
||
class LLM(Base): | ||
def __init__(self, rag, res_dict): | ||
self.model_name = "deepseek-chat" | ||
self.temperature = 0.1 | ||
self.top_p = 0.3 | ||
self.presence_penalty = 0.4 | ||
self.frequency_penalty = 0.7 | ||
self.max_tokens = 512 | ||
super().__init__(rag, res_dict) | ||
|
||
class Prompt(Base): | ||
def __init__(self, rag, res_dict): | ||
self.similarity_threshold = 0.2 | ||
self.keywords_similarity_weight = 0.7 | ||
self.top_n = 8 | ||
self.variables = [{"key": "knowledge", "optional": True}] | ||
self.rerank_model = None | ||
self.empty_response = None | ||
self.opener = "Hi! I'm your assistant, what can I do for you?" | ||
self.show_quote = True | ||
self.prompt = ( | ||
"You are an intelligent assistant. Please summarize the content of the knowledge base to answer the question. " | ||
"Please list the data in the knowledge base and answer in detail. When all knowledge base content is irrelevant to the question, " | ||
"your answer must include the sentence 'The answer you are looking for is not found in the knowledge base!' " | ||
"Answers need to consider chat history.\nHere is the knowledge base:\n{knowledge}\nThe above is the knowledge base." | ||
) | ||
super().__init__(rag, res_dict) | ||
|
||
def save(self) -> bool: | ||
res = self.post('/assistant/save', | ||
{"id": self.id, "name": self.name, "avatar": self.avatar, "knowledgebases": self.knowledgebases, | ||
"llm": self.llm.to_json(), "prompt": self.prompt.to_json() | ||
}) | ||
res = res.json() | ||
if res.get("retmsg") == "success": return True | ||
raise Exception(res["retmsg"]) | ||
|
||
def delete(self) -> bool: | ||
res = self.rm('/assistant/delete', | ||
{"id": self.id}) | ||
res = res.json() | ||
if res.get("retmsg") == "success": return True | ||
raise Exception(res["retmsg"]) | ||
|
||
def create_session(self, name: str = "New session", messages: List[Message] = [ | ||
{"role": "assistant", "reference": [], | ||
"content": "您好,我是您的助手小樱,长得可爱又善良,can I help you?"}]) -> Session: | ||
res = self.post("/session/save", {"name": name, "messages": messages, "assistant_id": self.id, }) | ||
res = res.json() | ||
if res.get("retmsg") == "success": | ||
return Session(self.rag, res['data']) | ||
raise Exception(res["retmsg"]) | ||
|
||
def get_prologue(self): | ||
return self.prompt.opener | ||
from typing import List | ||
|
||
from .base import Base | ||
from .session import Session | ||
|
||
|
||
class Assistant(Base): | ||
def __init__(self, rag, res_dict): | ||
self.id = "" | ||
self.name = "assistant" | ||
self.avatar = "path/to/avatar" | ||
self.knowledgebases = ["kb1"] | ||
self.llm = Assistant.LLM(rag, {}) | ||
self.prompt = Assistant.Prompt(rag, {}) | ||
super().__init__(rag, res_dict) | ||
|
||
class LLM(Base): | ||
def __init__(self, rag, res_dict): | ||
self.model_name = "deepseek-chat" | ||
self.temperature = 0.1 | ||
self.top_p = 0.3 | ||
self.presence_penalty = 0.4 | ||
self.frequency_penalty = 0.7 | ||
self.max_tokens = 512 | ||
super().__init__(rag, res_dict) | ||
|
||
class Prompt(Base): | ||
def __init__(self, rag, res_dict): | ||
self.similarity_threshold = 0.2 | ||
self.keywords_similarity_weight = 0.7 | ||
self.top_n = 8 | ||
self.variables = [{"key": "knowledge", "optional": True}] | ||
self.rerank_model = None | ||
self.empty_response = None | ||
self.opener = "Hi! I'm your assistant, what can I do for you?" | ||
self.show_quote = True | ||
self.prompt = ( | ||
"You are an intelligent assistant. Please summarize the content of the knowledge base to answer the question. " | ||
"Please list the data in the knowledge base and answer in detail. When all knowledge base content is irrelevant to the question, " | ||
"your answer must include the sentence 'The answer you are looking for is not found in the knowledge base!' " | ||
"Answers need to consider chat history.\nHere is the knowledge base:\n{knowledge}\nThe above is the knowledge base." | ||
) | ||
super().__init__(rag, res_dict) | ||
|
||
def save(self) -> bool: | ||
res = self.post('/assistant/save', | ||
{"id": self.id, "name": self.name, "avatar": self.avatar, "knowledgebases": self.knowledgebases, | ||
"llm": self.llm.to_json(), "prompt": self.prompt.to_json() | ||
}) | ||
res = res.json() | ||
if res.get("retmsg") == "success": return True | ||
raise Exception(res["retmsg"]) | ||
|
||
def delete(self) -> bool: | ||
res = self.rm('/assistant/delete', | ||
{"id": self.id}) | ||
res = res.json() | ||
if res.get("retmsg") == "success": return True | ||
raise Exception(res["retmsg"]) | ||
|
||
def create_session(self, name: str = "New session") -> Session: | ||
res = self.post("/session/save", {"name": name, "assistant_id": self.id}) | ||
res = res.json() | ||
if res.get("retmsg") == "success": | ||
return Session(self.rag, res['data']) | ||
raise Exception(res["retmsg"]) | ||
|
||
def list_session(self) -> List[Session]: | ||
res = self.get('/session/list', {"assistant_id": self.id}) | ||
res = res.json() | ||
if res.get("retmsg") == "success": | ||
result_list = [] | ||
for data in res["data"]: | ||
result_list.append(Session(self.rag, data)) | ||
return result_list | ||
raise Exception(res["retmsg"]) | ||
|
||
def get_session(self, id) -> Session: | ||
res = self.get("/session/get", {"id": id}) | ||
res = res.json() | ||
if res.get("retmsg") == "success": | ||
return Session(self.rag, res["data"]) | ||
raise Exception(res["retmsg"]) | ||
|
||
def get_prologue(self): | ||
return self.prompt.opener |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.