This commit is contained in:
2026-11-02 17:00:28 +08:00
parent 5d0f629439
commit 9ed39b51f7
43 changed files with 7163 additions and 151 deletions

View File

@@ -0,0 +1,19 @@
# AI 对话
import guan
response, history = guan.openai_chat("你好", model="qwen-plus") # 需要 API Key
print()
response = guan.langchain_chat_without_memory(prompt="你好", model="qwen-plus") # 需要 API Key
response == guan.langchain_chat_with_memory(prompt="你好", model="qwen-plus") # 需要 API Key
print()
response = guan.ollama_chat(prompt='你好', model="qwen2.5:0.5b") # 需要运行 Ollama 和下载对应的模型
print()
llm = guan.load_llama_model(model_path="D:/models/Qwen/Qwen3-0.6B-GGUF/Qwen3-0.6B-Q8_0.gguf") # 需要有模型文件
response, history = guan.llama_chat(llm, '你好 /no_think')
print()
model, tokenizer = guan.load_modelscope_model(model_name="D:/models/Qwen/Qwen3-0.6B") # 需要有模型文件
response, history = guan.modelscope_chat(model, tokenizer, '你好 /no_think')

View File

@@ -0,0 +1,7 @@
# 运行时间日志
import guan
import time
guan.logging_with_day_and_time(content='start')
for i in range(3):
time.sleep(5)
guan.logging_with_day_and_time(f'end_of_{i}')

View File

@@ -0,0 +1,25 @@
# 函数计时器
import guan
@guan.timer_decorator
def test1(a, b):
import time
print(a)
time.sleep(1)
print(b)
print('Run finished.')
for _ in range(2):
test1(10, b=20)
print()
def test2(a, b):
import time
print(a)
time.sleep(1)
print(b)
print('Run finished.')
for _ in range(2):
guan.timer(test2, 100, b=200)

View File

@@ -0,0 +1,23 @@
# try except 简写
import guan
@guan.try_decorator
def test1(a, b):
print(a)
bug_code
print(b)
return 'return_message1'
result1 = test1(10, b=20)
print(result1)
print()
def test2(a, b):
print(a)
bug_code
print(b)
return 'return_message2'
result2 = guan.try_except(test2, 100, b=200)
print(result2)

View File

@@ -0,0 +1,7 @@
# 变量写入文件
import guan
import numpy as np
data = np.array([1, 2, 3])
guan.dump_data(data, filename='a')
loaded_data = guan.load_data(filename='a')
print(loaded_data)

View File

@@ -0,0 +1,4 @@
# 打印数组
import guan
a = [1, 2, 3, 'a', 'b', 'c']
guan.print_array(a)

View File

@@ -0,0 +1,6 @@
# 矩阵写入文件后查看
import guan
import numpy as np
matrix = np.random.rand(5, 5)
guan.write_matrix_in_markdown_format(matrix=matrix, filename='markdown_matrix')
guan.write_matrix_in_latex_format(matrix=matrix, filename='latex_matrix')

View File

@@ -0,0 +1,15 @@
# 并行计算
import guan
import time
import os
def run_proc(name):
start_time = time.time()
time.sleep(5)
end_time = time.time()
print ('Process id running on name %s = %s' % (name, os.getpid()), '; running time = %s' % (end_time-start_time))
return f'name_{name}'
if __name__ == '__main__':
result_array = guan.parallel_calculation_with_multiprocessing_Pool(func=run_proc, args_list=range(32), show_time=1)
print(result_array)

View File

@@ -0,0 +1,33 @@
# 循环参数计算
import guan
import numpy as np
def test_1(x):
return 2*x
x_array = np.arange(0, 5, 1)
result_array = guan.loop_calculation_with_one_parameter(test_1, x_array)
print(result_array)
guan.plot(x_array, result_array)
print()
def test_2(x, y):
return x+y
x_array = np.arange(0, 5, 1)
y_array = np.arange(0, 3, 1)
result_array = guan.loop_calculation_with_two_parameters(test_2, x_array, y_array)
print(result_array)
guan.plot_contour(x_array, y_array, result_array)
print()
def test_3(x, y, z):
return x+y+z
x_array = np.arange(0, 5, 1)
y_array = np.arange(0, 3, 1)
z_array = np.arange(0, 2, 1)
result_array = guan.loop_calculation_with_three_parameters(test_3, x_array, y_array, z_array)
print(result_array)
guan.plot_contour(y_array, z_array, result_array[:, :, 4])

View File

@@ -0,0 +1,16 @@
# 检查是否为厄米矩阵
import guan
matrix1 = [
[2, 1.00001-1j],
[1+1j, 1]
]
print(guan.is_hermitian(matrix1))
matrix2 = [
[2, 1.00002-1j],
[1+1j, 1]
]
print(guan.is_hermitian(matrix2))

View File

@@ -0,0 +1,9 @@
# 数组分割
import numpy as np
import guan
task_num = 4
parameter_array_all = np.arange(0, 17, 1)
for task_index in range(task_num):
parameter_array = guan.preprocess_for_parallel_calculations(parameter_array_all, task_num, task_index)
print(parameter_array)
print()