|
|
@@ -0,0 +1,30 @@
|
|
|
+from config import load_config
|
|
|
+from zhipuai import ZhipuAI
|
|
|
+class Glm:
|
|
|
+ _instance = None
|
|
|
+ def __new__(cls, model_name):
|
|
|
+ if not cls._instance:
|
|
|
+ cls._instance = super(Glm, cls).__new__(cls)
|
|
|
+ cls._instance._initialized = False
|
|
|
+ return cls._instance
|
|
|
+
|
|
|
+ def __init__(self, model_name):
|
|
|
+ if not self._initialized:
|
|
|
+ self.cfg = load_config()["deepseek"]
|
|
|
+ self.client = ZhipuAI(api_key=self.cfg["api_key"])
|
|
|
+ self._initialized = True
|
|
|
+
|
|
|
+ self.model_name = model_name
|
|
|
+
|
|
|
+ def response(self, query):
|
|
|
+ resonse = self.client.chat.completions.create(
|
|
|
+ model=self.model_name,
|
|
|
+ messages=[
|
|
|
+ {"role": "user", "content": query}
|
|
|
+ ],
|
|
|
+ )
|
|
|
+ return resonse.choices[0].message
|
|
|
+
|
|
|
+if __name__ == '__main__':
|
|
|
+ glm = Glm("glm-4-flash")
|
|
|
+ glm.response("你好")
|