diff --git a/dsRag/Config/Config.py b/dsRag/Config/Config.py index d5365f11..1bc2ae76 100644 --- a/dsRag/Config/Config.py +++ b/dsRag/Config/Config.py @@ -28,5 +28,5 @@ DEEPSEEK_URL = 'https://api.deepseek.com' # 阿里云中用来调用 deepseek v3 的密钥【驿来特】 MODEL_API_KEY = "sk-f6da0c787eff4b0389e4ad03a35a911f" -MODEL_NAME = "qwen-plus" -#MODEL_NAME = "deepseek-v3" \ No newline at end of file +#MODEL_NAME = "qwen-plus" +MODEL_NAME = "deepseek-v3" \ No newline at end of file diff --git a/dsRag/Config/__pycache__/Config.cpython-310.pyc b/dsRag/Config/__pycache__/Config.cpython-310.pyc index dd640b9d..87b9ddcd 100644 Binary files a/dsRag/Config/__pycache__/Config.cpython-310.pyc and b/dsRag/Config/__pycache__/Config.cpython-310.pyc differ diff --git a/dsRag/Test/TestQWen3.py b/dsRag/Test/TestQWen3.py index d12fada8..f7e6f5b1 100644 --- a/dsRag/Test/TestQWen3.py +++ b/dsRag/Test/TestQWen3.py @@ -1,39 +1,10 @@ -import requests -from Config.Config import MODEL_API_KEY, MODEL_NAME - -class QWenClient: - def __init__(self): - self.api_key = MODEL_API_KEY - self.model_name = MODEL_NAME - self.base_url = "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation" - - def generate(self, prompt): - headers = { - "Authorization": f"Bearer {self.api_key}", - "Content-Type": "application/json" - } - - payload = { - "model": self.model_name, - "input": { - "messages": [ - {"role": "user", "content": prompt} - ] - } - } - - response = requests.post(self.base_url, json=payload, headers=headers) - response.raise_for_status() - return response.json() +from Util.ALiYunUtil import ALiYunUtil if __name__ == '__main__': - client = QWenClient() + ali_util = ALiYunUtil() while True: prompt = input("请输入问题(输入q退出): ") if prompt.lower() == 'q': break - try: - result = client.generate(prompt) - print("回答:", result['output']['text']) - except Exception as e: - print(f"发生错误: {str(e)}") \ No newline at end of file + answer = ali_util.chat(prompt) + print("回答:", answer) \ No newline at end of file diff --git a/dsRag/Util/ALiYunUtil.py b/dsRag/Util/ALiYunUtil.py new file mode 100644 index 00000000..8d88d746 --- /dev/null +++ b/dsRag/Util/ALiYunUtil.py @@ -0,0 +1,28 @@ +from openai import OpenAI +from Config.Config import MODEL_API_KEY, MODEL_NAME + +class ALiYunUtil: + def __init__(self): + self.client = OpenAI( + api_key=MODEL_API_KEY, + base_url="https://dashscope.aliyuncs.com/compatible-mode/v1" + ) + self.model_name = MODEL_NAME + + def chat(self, prompt, model=None): + """ + 与阿里云大模型对话 + :param prompt: 用户输入的问题 + :param model: 可选,指定使用的模型,默认使用Config中的MODEL_NAME + :return: 模型返回的答案 + """ + try: + completion = self.client.chat.completions.create( + model=model or self.model_name, + messages=[ + {'role': 'user', 'content': prompt} + ] + ) + return completion.choices[0].message.content + except Exception as e: + return f"发生错误: {str(e)}" \ No newline at end of file diff --git a/dsRag/Util/__pycache__/ALiYunUtil.cpython-310.pyc b/dsRag/Util/__pycache__/ALiYunUtil.cpython-310.pyc new file mode 100644 index 00000000..b3e93cbe Binary files /dev/null and b/dsRag/Util/__pycache__/ALiYunUtil.cpython-310.pyc differ