my_main.py 611 B

123456789101112131415161718192021
  1. # my_main.py
  2. from dotenv import load_dotenv
  3. from my_llm import MyLLM # 注意:这里导入我们自己的类
  4. # 加载环境变量
  5. load_dotenv()
  6. # 实例化我们重写的客户端,并指定provider
  7. llm = MyLLM(provider="modelscope")
  8. # 准备消息
  9. messages = [{"role": "user", "content": "你好,请介绍一下你自己。"}]
  10. # 发起调用,think等方法都已从父类继承,无需重写
  11. response_stream = llm.think(messages)
  12. # 打印响应
  13. print("ModelScope Response:")
  14. for chunk in response_stream:
  15. # chunk 已经是文本片段,可以直接使用
  16. print(chunk, end="", flush=True)