这个网址提供了多个大语言对话模型的 API 接口:https://chat.guanjihuan.com。本篇给出 Guan 软件包调用大语言模型的代码例子,会更灵活一些,例如自动对话、批量润色、批量翻译、批量提取信息、批量分析或分类等。如果不愿意折腾的,可以直接使用以上链接的对话界面,忽略本篇内容。
需要说明的是:guan.chat() 接口不支持上下文的功能。如果需要实现上下文对话,可以参考这篇:对话模型chat.guanjihuan.com的主要实现代码开源。
Guan 软件包网址:https://py.guanjihuan.com。安装方法为:pip install --upgrade guan 或 pip install --upgrade guan -i https://pypi.python.org/simple 。Guan 软件包的版本要求 >= 0.1.148。如果发现 guan.chat() 模型调用失效,可以等待一段时间再试,或联系我。
目前支持三个对话模型,之后可能不定期更新(默认为第一个模型):
- model=1:'hunyuan-lite'(转发服务商的 API)
- model=2:'deepseek-r1:1.5b' (运行在服务器 CPU 上的模型)
- model=3:'qwen2.5:1.5b' (运行在服务器 CPU 上的模型)
一、代码示例:最简单的使用
import guan
guan.chat('你好')
某次运行结果:
你好呀!很高兴能和你聊天,今天有什么想和我分享的吗😃
二、代码示例:默认的参数
import guan
response = guan.chat(prompt='你好', model=1, stream=1, stream_label=0)
print(response)
某次运行结果:
你好呀!很高兴能和你聊天,今天有什么想和我分享的吗😃
你好呀!很高兴能和你聊天,今天有什么想和我分享的吗😃
三、代码示例:机器人自动对话
import guan
response0 = '你好'
for i0 in range(2):
print(f'\n【对话第 {i0+1} 轮】\n')
print('机器人 1: ')
response1 = guan.chat(prompt=response0)
print('机器人 2: ')
response0 = guan.chat(prompt=response1)
某次运行结果:
【对话第 1 轮】
机器人 1:
你好!很高兴与你交流。请问有什么我可以帮助你的吗?无论是关于生活、工作、学习还是其他方面的问题,我都会尽力为你提供帮助。
机器人 2:
你好!非常高兴与你交流,你有什么问题需要我帮助么?
【对话第 2 轮】
机器人 1:
你好!我没有什么问题需要你帮助。作为一个AI助手我的目的是为用户提供有用的信息和回答问题,帮助人们解决问题和获取知识。如果你有任何问题或需要帮助,请随时问我,我会尽力为你提供最好的答案和支持。
机器人 2:
你好!很高兴能为你提供帮助。虽然你表示没有问题需要解答,但作为AI,我还是建议你随时向我提问。无论是关于生活、学习、工作还是其他方面的问题,我都会尽我所能为你提供信息和建议。请随时告诉我你的需求,我们一起学习和成长吧!
四、代码示例:引导机器人对话
import guan
response0 = '什么是神经网络?'
plus_message = '(回答字数少于50个字,最后反问我一个问题)'
for i0 in range(3):
print(f'\n【对话第 {i0+1} 轮】\n')
print('机器人 1: ')
response1 = guan.chat(prompt=response0+plus_message)
print('机器人 2: ')
response0 = guan.chat(prompt=response1+plus_message)
某次运行结果:
【对话第 1 轮】
机器人 1:
神经网络是一种模拟人脑神经元结构的计算模型,通过训练和学习来解决复杂问题。
机器人 2:
神经网络是模拟人脑神经元结构的计算模型,利用大量数据训练,使模型能够自动提取特征并解决问题。
【对话第 2 轮】
机器人 1:
神经网络确实是模拟人脑神经元结构的计算模型,通过大量数据训练来自动提取特征并解决问题。这种学习方式使得神经网络在图像识别、语音识别等领域具有强大的应用能力。那么,你对于神经网络的这种学习方式有什么看法呢?
机器人 2:
神经网络的学习方式非常强大,但也需要警惕过拟合等潜在问题。你认为如何确保神经网络在学习过程中保持稳健性?
【对话第 3 轮】
机器人 1:
为确保稳健性,可采取正则化、交叉验证、适量训练等策略,并持续监控模型性能。
机器人 2:
确实,这些策略能提升模型的稳健性和准确性。你有没有想过在特定任务中如何选择最合适的策略呢?
五、代码示例:批量处理/分句润色
import guan
content = "A topological insulator is a material with unique electronic properties. It behaves as an insulator in its interior but has conducting states on its surface. These surface states are protected by time-reversal symmetry and are robust against impurities and disorder. This means that electrons can flow on the surface without backscattering, even in the presence of defects or irregularities in the material. The special properties of topological insulators arise from their topological order, which is a global property of the material's electronic wavefunctions. This makes them interesting for potential applications in quantum computing and spintronics, where stable and efficient control of electronic states is crucial."
plus_message = '对以上文字进行学术英文润色。'
sentence_array = content.split('. ')
for sentence in sentence_array:
print('润色前的句子:', sentence+'. ')
print('润色后的句子:')
response = guan.chat(prompt=sentence+'. '+plus_message)
print()
某次运行结果:
润色前的句子: A topological insulator is a material with unique electronic properties.
润色后的句子:
A topological insulator is a distinctive class of materials characterized by its exceptional electronic properties, which differ fundamentally from those of conventional conductors and semiconductors.
润色前的句子: It behaves as an insulator in its interior but has conducting states on its surface.
润色后的句子:
The material exhibits insulating behavior within its interior, yet it possesses conducting states on its surface.
润色前的句子: These surface states are protected by time-reversal symmetry and are robust against impurities and disorder.
润色后的句子:
These surface states are safeguarded by time-reversal symmetry, rendering them resilient against impurities and disorder.
润色前的句子: This means that electrons can flow on the surface without backscattering, even in the presence of defects or irregularities in the material.
润色后的句子:
This implies that electrons are capable of moving along the surface without being scattered backward, even in the presence of imperfections or irregularities within the material.
润色前的句子: The special properties of topological insulators arise from their topological order, which is a global property of the material's electronic wavefunctions.
润色后的句子:
The unique characteristics of topological insulators are attributed to their topological order, a global property of the material's electronic wavefunctions.
润色前的句子: This makes them interesting for potential applications in quantum computing and spintronics, where stable and efficient control of electronic states is crucial..
润色后的句子:
This capability holds promise for potential applications in quantum computing and spintronics, where the stable and efficient manipulation of electronic states is paramount.
【说明:本站主要是个人的一些笔记和代码分享,内容可能会不定期修改。为了使全网显示的始终是最新版本,这里的文章未经同意请勿转载。引用请注明出处:https://www.guanjihuan.com】