MCPcopy Create free account
hub / github.com/InternLM/HuixiangDou / OpenXLabWorker

Class OpenXLabWorker

web/proxy/web_worker.py:59–308  ·  view source on GitHub ↗

The OpenXLab Worker class orchestrates the logic of handling user queries, generating responses and managing several aspects of a chat assistant. It enables feature storage, language model client setup, time scheduling and much more. Attributes: llm: A LLM instance that comm

Source from the content-addressed store, hash-verified

57
58
59class OpenXLabWorker:
60 """The OpenXLab Worker class orchestrates the logic of handling user queries,
61 generating responses and managing several aspects of a chat assistant. It
62 enables feature storage, language model client setup, time scheduling and
63 much more.
64
65 Attributes:
66 llm: A LLM instance that communicates with the language model.
67 fs: An instance of FeatureStore for loading and querying features.
68 config_path: A string indicating the path of the configuration file.
69 config: A dictionary holding the configuration settings.
70 language: A string indicating the language of the chat, default is 'zh' (Chinese). # noqa E501
71 context_max_length: An integer representing the maximum length of the context used by the language model. # noqa E501
72
73 Several template strings for various prompts are also defined.
74 """
75
76 def __init__(self, work_dir: str, config_path: str, language: str = 'zh'):
77 """Constructs all the necessary attributes for the worker object.
78
79 Args:
80 work_dir (str): The working directory where feature files are located.
81 config_path (str): The location of the configuration file.
82 language (str, optional): Specifies the language to be used. Defaults to 'zh' (Chinese). # noqa E501
83 """
84 self.llm = LLM(config_path=config_path)
85 self.config_path = config_path
86 self.config = None
87 self.language = language
88 with open(config_path, encoding='utf8') as f:
89 self.config = pytoml.load(f)
90 if self.config is None:
91 raise Exception('worker config can not be None')
92
93 self.context_max_length = -1
94 llm_config = self.config['llm']
95 self.context_max_length = llm_config['server']['remote_llm_max_text_length']
96
97 # Switch languages according to the scenario.
98 if self.language == 'zh':
99 self.TOPIC_TEMPLATE = '告诉我这句话的主题,直接说主题不要解释:“{}”'
100 self.SCORING_QUESTION_TEMPLATE = '“{}”\n请仔细阅读以上内容,判断句子是否是个疑问句,结果用 0~10 表示。直接提供得分不要解释。\n判断标准:有主语谓语宾语并且是疑问句得 10 分;缺少主谓宾扣分;陈述句直接得 0 分;不是疑问句直接得 0 分。直接提供得分不要解释。' # noqa E501
101 self.SCORING_RELAVANCE_TEMPLATE = '问题:“{}”\n材料:“{}”\n请仔细阅读以上内容,判断问题和材料的关联度,用0~10表示。判断标准:非常相关得 10 分;完全没关联得 0 分。直接提供得分不要解释。\n' # noqa E501
102 self.KEYWORDS_TEMPLATE = '谷歌搜索是一个通用搜索引擎,可用于访问互联网、查询百科知识、了解时事新闻等。搜索参数类型 string, 内容是短语或关键字,以空格分隔。\n你现在是{}交流群里的技术助手,用户问“{}”,你打算通过谷歌搜索查询相关资料,请提供用于搜索的关键字或短语,不要解释直接给出关键字或短语。' # noqa E501
103 self.SECURITY_TEMAPLTE = '判断以下句子是否涉及政治、辱骂、色情、恐暴、宗教、网络暴力、种族歧视等违禁内容,结果用 0~10 表示,不要解释直接给出得分。判断标准:涉其中任一问题直接得 10 分;完全不涉及得 0 分。直接给得分不要解释:“{}”' # noqa E501
104 self.PERPLESITY_TEMPLATE = '“question:{} answer:{}”\n阅读以上对话,answer 是否在表达自己不知道,回答越全面得分越少,用0~10表示,不要解释直接给出得分。\n判断标准:准确回答问题得 0 分;答案详尽得 1 分;知道部分答案但有不确定信息得 8 分;知道小部分答案但推荐求助其他人得 9 分;不知道任何答案直接推荐求助别人得 10 分。直接打分不要解释。' # noqa E501
105 self.SUMMARIZE_TEMPLATE = '{} \n 仔细阅读以上内容,总结得简短有力点' # noqa E501
106 # self.GENERATE_TEMPLATE = '材料:“{}”\n 问题:“{}” \n 请仔细阅读参考材料回答问题,材料可能和问题无关。如果材料和问题无关,尝试用你自己的理解来回答问题。如果无法确定答案,直接回答不知道。' # noqa E501
107 self.GENERATE_TEMPLATE = '材料:“{}”\n 问题:“{}” \n 请仔细阅读参考材料回答问题。' # noqa E501
108 else:
109 self.TOPIC_TEMPLATE = 'Tell me the theme of this sentence, just state the theme without explanation: "{}"' # noqa E501
110 self.SCORING_QUESTION_TEMPLATE = '"{}"\nPlease read the content above carefully and judge whether the sentence is a thematic question. Rate it on a scale of 0-10. Only provide the score, no explanation.\nThe criteria are as follows: a sentence gets 10 points if it has a subject, predicate, object and is a question; points are deducted for missing subject, predicate or object; declarative sentences get 0 points; sentences that are not questions also get 0 points. Just give the score, no explanation.' # noqa E501
111 self.SCORING_RELAVANCE_TEMPLATE = 'Question: "{}", Background Information: "{}"\nPlease read the content above carefully and assess the relevance between the question and the material on a scale of 0-10. The scoring standard is as follows: extremely relevant gets 10 points; completely irrelevant gets 0 points. Only provide the score, no explanation needed.' # noqa E501
112 self.KEYWORDS_TEMPLATE = 'Google search is a general-purpose search engine that can be used to access the internet, look up encyclopedic knowledge, keep abreast of current affairs and more. Search parameters type: string, content consists of phrases or keywords separated by spaces.\nYou are now the assistant in the "{}" communication group. A user asked "{}", you plan to use Google search to find related information, please provide the keywords or phrases for the search, no explanation, just give the keywords or phrases.' # noqa E501
113 self.SECURITY_TEMAPLTE = 'Evaluate whether the following sentence involves prohibited content such as politics, insult, pornography, terror, religion, cyber violence, racial discrimination, etc., rate it on a scale of 0-10, do not explain, just give the score. The scoring standard is as follows: any violation directly gets 10 points; completely unrelated gets 0 points. Give the score, no explanation: "{}"' # noqa E501
114 self.PERPLESITY_TEMPLATE = 'Question: {} Answer: {}\nRead the dialogue above, does the answer express that they don\'t know? The more comprehensive the answer, the lower the score. Rate it on a scale of 0-10, no explanation, just give the score.\nThe scoring standard is as follows: an accurate answer to the question gets 0 points; a detailed answer gets 1 point; knowing some answers but having uncertain information gets 8 points; knowing a small part of the answer but recommends seeking help from others gets 9 points; not knowing any of the answers and directly recommending asking others for help gets 10 points. Just give the score, no explanation.' # noqa E501
115 self.SUMMARIZE_TEMPLATE = '"{}" \n Read the content above carefully, summarize it in a short and powerful way.' # noqa E501
116 self.GENERATE_TEMPLATE = 'Background Information: "{}"\n Question: "{}"\n Please read the reference material carefully and answer the question.' # noqa E501

Callers 2

web_worker.pyFile · 0.85
chat_with_featue_storeFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected