Compare commits

...

5 Commits

Author SHA1 Message Date
5c055bfe8b 0.1.148 2025-02-22 15:33:13 +08:00
78898443c3 0.1.147 2025-02-22 11:32:09 +08:00
65ae0be518 0.1.145 2025-02-22 00:41:33 +08:00
e171790a15 0.1.144 2025-02-20 05:39:30 +08:00
dfeb15c963 0.1.143 2025-02-20 05:35:22 +08:00
3 changed files with 50 additions and 126 deletions

View File

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