This commit is contained in:
guanjihuan 2024-12-26 21:17:59 +08:00
parent e132cd8f75
commit 24db487104
3 changed files with 39 additions and 7 deletions

View File

@ -1,7 +1,7 @@
[metadata] [metadata]
# replace with your username: # replace with your username:
name = guan name = guan
version = 0.1.137 version = 0.1.138
author = guanjihuan author = guanjihuan
author_email = guanjihuan@163.com author_email = guanjihuan@163.com
description = An open source python package description = An open source python package

View File

@ -1,6 +1,6 @@
Metadata-Version: 2.1 Metadata-Version: 2.1
Name: guan Name: guan
Version: 0.1.137 Version: 0.1.138
Summary: An open source python package Summary: An open source python package
Home-page: https://py.guanjihuan.com Home-page: https://py.guanjihuan.com
Author: guanjihuan Author: guanjihuan

View File

@ -1,6 +1,6 @@
# Module: data_processing # Module: data_processing
# AI模型对话 # AI 对话
def chat(prompt='你好', stream=1, model=1, top_p=0.8, temperature=0.85): def chat(prompt='你好', stream=1, model=1, top_p=0.8, temperature=0.85):
import socket import socket
import json import json
@ -37,9 +37,9 @@ def chat(prompt='你好', stream=1, model=1, top_p=0.8, temperature=0.85):
} }
send_message = json.dumps(message) send_message = json.dumps(message)
client_socket.send(send_message.encode('utf-8')) client_socket.send(send_message.encode('utf-8'))
time.sleep(0.15) time.sleep(0.2)
if stream == 1: if stream == 1:
print('\n--- Begin Stream Message ---\n') print('\n--- Begin Chat Stream Message ---\n')
response = '' response = ''
while True: while True:
if prompt == '': if prompt == '':
@ -61,9 +61,41 @@ def chat(prompt='你好', stream=1, model=1, top_p=0.8, temperature=0.85):
break break
client_socket.close() client_socket.close()
if stream == 1: if stream == 1:
print('\n\n--- End Stream Message ---\n') print('\n\n--- End Chat Stream Message ---\n')
return response return response
# 加上函数代码的 AI 对话
def chat_with_function_code(function_name, prompt='', stream=1, model=1, top_p=0.8, temperature=0.85):
import guan
function_source = guan.get_source(function_name)
if prompt == '':
response = guan.chat(prompt=function_source, stream=stream, model=model, top_p=top_p, temperature=temperature)
else:
response = guan.chat(prompt=function_source+'\n\n'+prompt, stream=stream, model=model, top_p=top_p, temperature=temperature)
return response
# 机器人自动对话
def auto_chat(prompt='你好', round=2):
import guan
response0 = prompt
for i0 in range(round):
print(f'【对话第 {i0+1} 轮】\n')
print('机器人 1: ')
response1 = guan.chat(prompt=response0, stream=1)
print('机器人 2: ')
response0 = guan.chat(prompt=response1, stream=1)
# 机器人自动对话(引导对话)
def auto_chat_with_guide(prompt='你好', guide_message='回答字数少于30个字最后反问我一个问题', round=5):
import guan
response0 = prompt
for i0 in range(round):
print(f'【对话第 {i0+1} 轮】\n')
print('机器人 1: ')
response1 = guan.chat(prompt=response0+guide_message, stream=1)
print('机器人 2: ')
response0 = guan.chat(prompt=response1+guide_message, stream=1)
# 在云端服务器上运行函数(需要函数是独立可运行的代码) # 在云端服务器上运行函数(需要函数是独立可运行的代码)
def run(function_name, *args, **kwargs): def run(function_name, *args, **kwargs):
import socket import socket
@ -103,7 +135,7 @@ def run(function_name, *args, **kwargs):
} }
send_message = json.dumps(message) send_message = json.dumps(message)
client_socket.send(send_message.encode()) client_socket.send(send_message.encode())
time.sleep(0.15) time.sleep(0.2)
print('\nguan.run: 云端服务器正在计算,请等待返回结果。\n') print('\nguan.run: 云端服务器正在计算,请等待返回结果。\n')
return_data = '' return_data = ''
print_data = '' print_data = ''