| 12345678910111213141516171819202122232425262728293031 |
- 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()['glm']
- 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")
- response = glm.response("请帮我编写一段快速排序的代码")
- print(response)
|