Compare commits

..

No commits in common. "5c055bfe8b1ab2b28acf8d3dc6ffdd3348d88d54" and "5dbbc968e2d38eb70d5b38bc30dbc4813034ca8e" have entirely different histories.

3 changed files with 126 additions and 50 deletions

View File

@ -1,7 +1,7 @@
[metadata] [metadata]
# replace with your username: # replace with your username:
name = guan name = guan
version = 0.1.148 version = 0.1.142
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.2 Metadata-Version: 2.2
Name: guan Name: guan
Version: 0.1.148 Version: 0.1.142
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,40 +1,77 @@
# Module: data_processing # Module: data_processing
# AI 对话 # AI 对话
def chat(prompt='你好', model=1, stream=1, stream_label=0): def chat(prompt='你好', stream=1, model=1, top_p=0.8, temperature=0.85):
import requests import socket
url = "http://api.guanjihuan.com/chat" import json
data = { import time
"prompt": prompt, import guan
"model": model, with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client_socket:
} client_socket.settimeout(30)
if stream == 1: client_socket.connect(('socket.guanjihuan.com', 12345))
if stream_label == 1: split_text_list = guan.split_text(prompt, width=100)
message_times = len(split_text_list)
if message_times == 1 or message_times == 0:
message = {
'server': "chat.guanjihuan.com",
'prompt': prompt,
'model': model,
'top_p': top_p,
'temperature': temperature,
}
send_message = json.dumps(message)
client_socket.send(send_message.encode('utf-8'))
else:
end_message = 0
for i0 in range(message_times):
if i0 == message_times-1:
end_message = 1
prompt_0 = split_text_list[i0]
message = {
'server': "chat.guanjihuan.com",
'prompt': prompt_0,
'model': model,
'top_p': top_p,
'temperature': temperature,
'end_message': end_message,
}
send_message = json.dumps(message)
client_socket.send(send_message.encode('utf-8'))
time.sleep(0.2)
if stream == 1:
print('\n--- Start Chat Stream Message ---\n') print('\n--- Start Chat Stream Message ---\n')
requests_response = requests.post(url, json=data, stream=True) response = ''
response = '' while True:
if requests_response.status_code == 200: if prompt == '':
for line in requests_response.iter_lines(): break
if line: try:
if stream == 1: data = client_socket.recv(1024)
print(line.decode('utf-8'), end='', flush=True) if data != b'':
response += line.decode('utf-8') response_data = data.decode()
print() response_dict = json.loads(response_data)
else: stream_response = response_dict['stream_response']
pass response += stream_response
if stream == 1: end_message = response_dict['end_message']
if stream_label == 1: if end_message == 1:
print('\n--- End Chat Stream Message ---\n') break
else:
if stream == 1:
print(stream_response, end='', flush=True)
except:
break
# client_socket.close()
if stream == 1:
print('\n\n--- End Chat Stream Message ---\n')
return response return response
# 加上函数代码的 AI 对话 # 加上函数代码的 AI 对话
def chat_with_function_code(function_name, prompt='', model=1, stream=1): def chat_with_function_code(function_name, prompt='', stream=1, model=1, top_p=0.8, temperature=0.85):
import guan import guan
function_source = guan.get_source(function_name) function_source = guan.get_source(function_name)
if prompt == '': if prompt == '':
response = guan.chat(prompt=function_source, model=model, stream=stream) response = guan.chat(prompt=function_source, stream=stream, model=model, top_p=top_p, temperature=temperature)
else: else:
response = guan.chat(prompt=function_source+'\n\n'+prompt, model=model, stream=stream) response = guan.chat(prompt=function_source+'\n\n'+prompt, stream=stream, model=model, top_p=top_p, temperature=temperature)
return response return response
# 机器人自动对话 # 机器人自动对话
@ -61,29 +98,68 @@ def auto_chat_with_guide(prompt='你好', guide_message='回答字数少于30
# 在云端服务器上运行函数(需要函数是独立可运行的代码) # 在云端服务器上运行函数(需要函数是独立可运行的代码)
def run(function_name, *args, **kwargs): def run(function_name, *args, **kwargs):
import requests import socket
import json
import pickle
import base64
import time
import guan import guan
url = "http://run.guanjihuan.com/run_function" with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client_socket:
function_source = guan.get_source(function_name) client_socket.connect(('run.guanjihuan.com', 12345))
data = { function_source = guan.get_source(function_name)
"function_name": function_name.__name__, split_text_list = guan.split_text(function_source, width=100)
"function_source": function_source, message_times = len(split_text_list)
'args': str(args), if message_times == 1 or message_times == 0:
'kwargs': str(kwargs), message = {
} 'server': "run.guanjihuan.com",
return_data = None 'function_name': function_name.__name__,
try: 'function_source': function_source,
response = requests.post(url, json=data) 'args': str(args),
if response.status_code == 200: 'kwargs': str(kwargs)
result = response.json() }
print_data = result['print_data'] send_message = json.dumps(message)
print(print_data, end='') client_socket.send(send_message.encode())
encoded_return_data = result['encoded_return_data'] else:
import base64 end_message = 0
import pickle for i0 in range(message_times):
return_data = pickle.loads(base64.b64decode(encoded_return_data)) if i0 == message_times-1:
except: end_message = 1
pass source_0 = split_text_list[i0]
message = {
'server': "run",
'function_name': function_name.__name__,
'function_source': source_0,
'args': str(args),
'kwargs': str(kwargs),
'end_message': end_message,
}
send_message = json.dumps(message)
client_socket.send(send_message.encode())
time.sleep(0.2)
print('\nguan.run: 云端服务器正在计算,请等待返回结果。\n')
return_data = ''
print_data = ''
while True:
try:
data = client_socket.recv(1024)
return_text = data.decode()
return_dict = json.loads(return_text)
return_data += return_dict['return_data']
print_data += return_dict['print_data']
end_message = return_dict['end_message']
if end_message == 1:
break
except:
break
if print_data != '':
print('--- Start Print ---\n')
print(print_data)
print('--- End Print ---\n')
print('guan.run: 云端服务器计算结束,以上是打印结果。\n')
else:
print('guan.run: 云端服务器计算结束。\n')
return_data = pickle.loads(base64.b64decode(return_data))
# client_socket.close()
return return_data return return_data
# 将XYZ数据转成矩阵数据说明x_array/y_array的输入和输出不一样。要求z_array数据中y对应的数据为小循环x对应的数据为大循环 # 将XYZ数据转成矩阵数据说明x_array/y_array的输入和输出不一样。要求z_array数据中y对应的数据为小循环x对应的数据为大循环