学术, 其他笔记

使用 guan.chat() 函数实现AI对话

这个网址提供了多个大语言对话模型的 API 接口:https://chat.guanjihuan.com。虽然智能程度不及 ChatGPT, Claude, Gemini, GPT4 等最先进的模型,但对于翻译、润色,以及简单的一些问题,还是足够用的,且可以根据不同模型的回答进行综合判断和筛选。

本篇给出 Guan 软件包调用大语言模型的代码例子,会更灵活一些,例如自动对话、批量润色、批量翻译、批量提取信息、批量分析或分类等。如果不愿意折腾的,可以直接使用以上链接的对话界面,忽略本篇内容。

需要说明的是:guan.chat() 接口不支持上下文的功能。如果需要实现上下文对话,可以参考这篇:对话模型chat.guanjihuan.com的主要实现代码开源

Guan 软件包网址:https://py.guanjihuan.com。安装方法为:pip install --upgrade guan -i https://pypi.python.org/simple 或者 pip install --upgrade guan 。Guan 软件包的版本要求 >= 0.1.135。如果发现 guan.chat() 模型调用失效,可联系我。

目前支持五个对话模型,之后可能不定期更新(默认为第一个模型):

  • model=1:'hunyuan-lite'
  • model=2:'qwen1.5-0.5b-chat'
  • model=3:'qwen-1.8b-chat'
  • model=4:'ernie-tiny-8k'
  • model=5:'ernie-lite-8k'

一、代码示例:最简单的使用

import guan
guan.chat('你好')

某次运行结果:

--- Begin Stream Message ---

你好!很高兴与你交流。请问有什么我可以帮助你的吗?无论是关于生活、工作、学习还是其他方面的问题,我都会尽力回答你。

--- End Stream Message ---

二、代码示例:默认的参数

import guan
response = guan.chat(prompt='你好', stream=1, model=1, top_p=0.8, temperature=0.85)
print(response)

某次运行结果:

--- Begin Stream Message ---

你好!很高兴与你交流。请问有什么我可以帮助你的吗?无论是关于生活、工作、学习还是其他方面的问题,都可以随时向我提问哦。

--- End Stream Message ---

你好!很高兴与你交流。请问有什么我可以帮助你的吗?无论是关于生活、工作、学习还是其他方面的问题,都可以随时向我提问哦。

三、代码示例:机器人自动对话

import guan
response0 = '你好'
for i0 in range(2):
    print(f'【对话第 {i0+1} 轮】\n')
    print('机器人 1: ')
    response1 = guan.chat(prompt=response0, stream=1)
    print('机器人 2: ')
    response0 = guan.chat(prompt=response1, stream=1)

某次运行结果:

【对话第 1 轮】

机器人 1: 

--- Begin Stream Message ---

你好!很高兴与你交流。请问有什么我可以帮助你的吗?无论是关于生活、工作、学习还是其他方面的问题,我都会尽力回答你。

--- End Stream Message ---

机器人 2: 

--- Begin Stream Message ---

你好!非常高兴与你交流,无论是分享困扰、寻求建议还是讨论各种话题,我都会尽力提供帮助。请随时告诉我你的需求,我会尽我所能为你提供支持和建议。

--- End Stream Message ---

【对话第 2 轮】

机器人 1: 

--- Begin Stream Message ---

你好!很高兴与你交流,无论是分享困扰、寻求建议还是讨论各种话题,我都会尽力提供帮助。请随时告诉我你的需求,我会尽我所能为你提供支持和建议。

有什么我可以帮到你的吗?请随时告诉我,我会竭诚为你服务。

--- End Stream Message ---

机器人 2: 

--- Begin Stream Message ---

你好!很高兴与你交流,无论是分享困扰、寻求建议还是讨论各种话题,我都会尽力提供帮助。请随时告诉我你的需求,我会尽我所能为你提供支持和建议。

有什么我可以帮到你的吗?请随时告诉我,我会竭诚为你服务。

--- End Stream Message ---

四、代码示例:引导机器人对话

import guan
response0 = '什么是神经网络?'
plus_message = '(回答字数少于30个字,最后反问我一个问题)'
for i0 in range(3):
    print(f'【对话第 {i0+1} 轮】\n')
    print('机器人 1: ')
    response1 = guan.chat(prompt=response0+plus_message, stream=1)
    print('机器人 2: ')
    response0 = guan.chat(prompt=response1+plus_message, stream=1)

某次运行结果:

【对话第 1 轮】

机器人 1: 

--- Begin Stream Message ---

神经网络是一种模拟人脑神经元连接方式的计算模型,用于处理复杂数据和模式识别。

--- End Stream Message ---

机器人 2: 

--- Begin Stream Message ---

对,神经网络通过模拟神经元连接进行计算,擅长处理复杂数据和识别模式,广泛应用于图像识别、语音识别等领域。

--- End Stream Message ---

【对话第 2 轮】

机器人 1: 

--- Begin Stream Message ---

神经网络确实强大且多才多艺,你有没有遇到过相关的问题呢?

--- End Stream Message ---

机器人 2: 

--- Begin Stream Message ---

确实强大,常遇训练超参数、模式识别等问题。

--- End Stream Message ---

【对话第 3 轮】

机器人 1: 

--- Begin Stream Message ---

训练超参数和模式识别确实强大,你最近在模型训练上有何进展?

--- End Stream Message ---

机器人 2: 

--- Begin Stream Message ---

模型训练有进步,但具体成果需进一步展示。

--- End Stream Message ---

五、代码示例:批量处理/分句润色

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, stream=1)
    print('\n\n\n')

某次运行结果:

润色前的句子: A topological insulator is a material with unique electronic properties. 
润色后的句子:

--- Begin Stream Message ---

A topological insulator is a distinctive type of material characterized by its unique electronic properties, which differ fundamentally from those of conventional conductors and semiconductors.

--- End Stream Message ---





润色前的句子: It behaves as an insulator in its interior but has conducting states on its surface. 
润色后的句子:

--- Begin Stream Message ---

It exhibits insulating behavior within its interior, yet possesses conducting states on its surface.

--- End Stream Message ---





润色前的句子: These surface states are protected by time-reversal symmetry and are robust against impurities and disorder. 
润色后的句子:

--- Begin Stream Message ---

These surface states are safeguarded by time-reversal symmetry and exhibit a high degree of robustness against impurities and disorder.

--- End Stream Message ---





润色前的句子: This means that electrons can flow on the surface without backscattering, even in the presence of defects or irregularities in the material. 
润色后的句子:

--- Begin Stream Message ---

This implies that electrons are capable of flowing over the surface of the material without undergoing backscattering, even in the presence of defects or irregularities.

--- End Stream Message ---





润色前的句子: The special properties of topological insulators arise from their topological order, which is a global property of the material's electronic wavefunctions. 
润色后的句子:

--- Begin Stream Message ---

The unique characteristics of topological insulators are attributed to their topological order, a global property associated with the electronic wavefunctions of the material.

--- End Stream Message ---





润色前的句子: This makes them interesting for potential applications in quantum computing and spintronics, where stable and efficient control of electronic states is crucial.. 
润色后的句子:

--- Begin Stream Message ---

This characteristic renders them particularly intriguing for potential applications in the fields of quantum computing and spintronics, where the stable and efficient manipulation of electronic states is of paramount importance.

--- End Stream Message ---
309 次浏览

【说明:本站主要是个人的一些笔记和代码分享,内容可能会不定期修改。为了使全网显示的始终是最新版本,这里的文章未经同意请勿转载。引用请注明出处:https://www.guanjihuan.com

发表评论

您的邮箱地址不会被公开。 必填项已用 * 标注

Captcha Code