-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdebug.py
More file actions
130 lines (106 loc) · 12.1 KB
/
Copy pathdebug.py
File metadata and controls
130 lines (106 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import time
import random
from _global import *
from chat import Chat
class Delta:
def __init__(self, c, r):
if r:
self.reasoning_content = c
else:
self.content = c
class Choice:
def __init__(self, c, r):
self.delta = Delta(c, r)
class Chunk:
def __init__(self, text, reasoning=False):
self.choices = [
Choice(text, reasoning)
]
tt = [
"以下是将你的 Bash 脚本转换为 PowerShell 脚本的版本:\n\n```powershell\n# 检查是否安装了 Python3\nif (-not (Get-Command python3 -ErrorAction SilentlyContinue)) {\n Write-Host \"Installing Python3\"\n sudo apt-get install python3\n}\n\n# 检查是否安装了 pip3\nif (-not (Get-Command pip3 -ErrorAction SilentlyContinue)) {\n Write-Host \"Installing pip3\"\n sudo apt-get install python3-pip\n}\n\n# 检查是否安装了 openai 包\nif (-not (pip show openai -ErrorAction SilentlyContinue)) {\n Write-Host \"Installing openai\"\n pip3 install openai\n}\n\nWrite-Host \"--------------------\"\nWrite-Host \"Setting up the working directory\"\n$ROOT_DIR = Get-Location\n$ROOT_DIR_R = $ROOT_DIR.Path.Replace(\"\\\", \"\\\\\")\n$ROOT_DIR_REX = '1,$s/(ROOT_DIR\\s=\\sPath\\(\\\").*?(\\\"\\))/\\1' + $ROOT_DIR_R + '\\2/'\n(Get-Content ai.py) -replace 'ROOT_DIR\\s=\\sPath\\(\\\".*?\\\"\\)', \"ROOT_DIR = Path(`\"$ROOT_DIR`\")\" | Set-Content ai.py\n\n# 检查是否存在 config.json 文件\nif (Test-Path config.json) {\n Write-Host \"`config.json` already exists!\"\n} else {\n Write-Host \"Creating config.json\"\n @'\n{\n \"api_key\": \"\",\n \"base_url\": \"\",\n \"system_prompt\": \"你是一个熟悉计算机并具有丰富教学经验的 AI 助手,无论提出问题的是什么语言,请用中文回答我的问题\",\n \"models\": [\n {\n \"model\": \"deepseek-v3\",\n \"alias\": [\"v3\"]\n },\n {\n \"model\": \"deepseek-r1\",\n \"alias\": [\"r1\"]\n }\n ],\n \"model\": \"deepseek-v3\",\n \"temperature\": 0.5,\n \"max_history\": 100\n}\n'@ | Set-Content config.json\n}\n\nWrite-Host \"--------------------\"\nwhile ($true) {\n $answer = Read-Host \"Do you want to add alias `ag` to your PowerShell profile? (y/n)\"\n $answer = $answer.ToLower()\n\n if ($answer -eq \"y\") {\n $pythonPath = (Get-Command python3).Source\n $aliasCommand = \"function ag { & '$pythonPath' '$ROOT_DIR\\ai.py' @args }\"\n Add-Content -Path $PROFILE -Value $aliasCommand\n . $PROFILE\n break\n } elseif ($answer -eq \"n\") {\n Write-Host \"Skipping...\"\n break\n } else {\n Write-Host \"Invalid input, please enter y or n.\"\n }\n}\n\nWrite-Host \"--------------------\"\nWrite-Host -ForegroundColor Green -BackgroundColor Black \"Installation complete!\"\nWrite-Host -ForegroundColor Blue -BackgroundColor Black \"Please fill in the `config.json` file with your API key and base URL.\"\nWrite-Host \"You can run the program by:\"\nWrite-Host \" 1. `python3 ai.py`\"\nWrite-Host \" 2. `chmod +x ai.py` and then `.\\ai.py`\"\n```\n\n### 主要更改点:\n1. **命令替换**:\n - `which` 替换为 `Get-Command`。\n - `sed` 替换为 PowerShell 的字符串替换操作。\n - `echo` 替换为 `Write-Host`。\n - `read` 替换为 `Read-Host`。\n\n2. **路径处理**:\n - `pwd` 替换为 `Get-Location`。\n - 路径分隔符 `\\` 在 PowerShell 中需要转义为 `\\\\`。\n\n3. **别名添加**:\n - 在 PowerShell 中,别名通过 `function` 定义,并添加到 `$PROFILE` 文件中。\n\n4. **颜色输出**:\n - 使用 `Write-Host` 的 `-ForegroundColor` 和 `-BackgroundColor` 参数来实现颜色输出。\n\n### 注意事项:\n- 该脚本假设你在 Windows 上使用 PowerShell,并且已经安装了 Python3 和 pip3。\n- 如果你在 Linux 或 macOS 上使用 PowerShell Core,可能需要调整部分命令(如 `sudo apt-get`)。",
"```python\nprint('abc')\n```\n另一端代码\n```js\nconsole.log(\"def\")\n```\n再看看 bash\n```bash\necho xxx\n```\n",
"看起来你的系统中没有安装 `lsblk` 工具。`lsblk` 通常预装在 Linux 系统中,但如果你使用的是 Windows 系统,默认是没有这个工具的。\n\n### 解决方案\n1. **如果你使用的是 Linux 系统**:\n - 确保 `lsblk` 已安装。你可以通过以下命令安装它:\n ```bash\n sudo apt-get install util-linux\n ```\n - 安装后,再次运行 `lsblk`。\n\n2. **如果你使用的是 Windows 系统**:\n - `lsblk` 是 Linux 工具,Windows 上没有直接等效的工具。你可以使用以下替代方法:\n - 使用 `diskpart` 命令查看磁盘信息:\n ```bash\n diskpart\n list disk\n ```\n - 或者使用 PowerShell 命令:\n ```powershell\n Get-Disk\n ```\n\n### 示例(Linux)\n假设你已安装 `lsblk`,以下是一个示例输出:\n```bash\nNAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT\nsda 8:0 0 100G 0 disk \n├─sda1 8:1 0 50G 0 part /\n└─sda2 8:2 0 50G 0 part /home\n```\n\n如果你需要进一步帮助,请告诉我你的操作系统环境!",
"`lsblk` 是一个用于列出块设备信息的命令行工具。它显示系统中所有块设备(如硬盘、分区、挂载点等)的树状结构,帮助用户快速了解存储设备的布局。\n\n### 基本用法\n```bash\nlsblk\n```\n\n### 常用选项\n- `-a`:显示所有设备,包括空设备。\n- `-f`:显示文件系统类型。\n- `-o`:指定输出的列(如 NAME, SIZE, FSTYPE, MOUNTPOINT 等)。\n- `-p`:显示完整设备路径(如 `/dev/sda1`)。\n\n### 示例\n1. **列出所有块设备**:\n ```bash\n lsblk\n ```\n\n 输出示例:\n ```\n NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT\n sda 8:0 0 100G 0 disk \n ├─sda1 8:1 0 50G 0 part /\n └─sda2 8:2 0 50G 0 part /home\n ```\n\n2. **显示文件系统类型**:\n ```bash\n lsblk -f\n ```\n 输出示例:\n ```\n NAME FSTYPE LABEL UUID MOUNTPOINT\n sda \n ├─sda1 ext4 root c1b9d5a2-3e7f-4b1e-8e3a-9c8b7d6e5f4a /\n └─sda2 ext4 home d2e8f9a1-4b6c-4d7e-8f2a-1b3c4d5e6f7b /home\n ```\n\n3. **自定义输出列**:\n ```bash\n lsblk -o NAME,SIZE,MOUNTPOINT\n ```\n 输出示例:\n ```\n NAME SIZE MOUNTPOINT\n sda 100G \n ├─sda1 50G /\n └─sda2 50G /home\n ```\n\n`lsblk` 是一个简单但功能强大的工具,适合快速查看系统存储设备的状态。",
"### 示例\n1. **列出所有块设备**:\n ```bash\n lsblk\n ```\n 输出示例:\n ```\n NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT\n sda 8:0 0 100G 0 disk \n ├─sda1 8:1 0 50G 0 part /\n └─sda2 8:2 0 50G 0 part /home\n ```\n\n",
"- 列表\n - 列表\n - 列表\n - 列表\n\n1. 列表\n 1. 列表\n 1. 列表\n 1. 列表\n\n",
r'''> 2025.3.21 231880291 陈柏均
# 习题 7
> 求 $f(x)=e^x$ 在 $[0,1]$ 上的最佳一次逼近多项式
解:设最佳一次逼近多项式为 $P_{1}(x)=ax+b$,则由 Chebyshev 定理,至少有三个点 $0 \le x_{1}<x_{2}<x_{3} \le 1$ 使得 $P_{1}(x_k)-f(x_k)=(-1)^k\sigma \max_{0 \le x \le 1}|P_{1}(x)-f(x)|(\sigma=\pm1,k=1,2,3)$。由函数连续性,$P_{1}(x)-f(x)$ 在 $[0,1]$ 至少有两个零点。由 Rolle 定理,$P_{1}'(x)-f'(x)=a-e^x$ 在 $[0,1]$ 内至少有一个零点。由于 $f''(x)=e^x>0$,故 $f'(x)$ 单调,$a-e^x$ 在 $[0,1]$ 有且仅有一个零点。由导函数连续性,必有 $P_{1}'(x_{2})-f'(x_{2})=a-e^{x_{2}}=0,x_{1}=0,x_{3}=1$,故 $e^{x_{2}}=a,P_{1}(0)-e^0=P_{1}(1)-e=-P_{1}(x_{2})+e^{x_{2}}$。从而可得 $b-1=a+b-e=-ax_{2}+e^{x_{2}},a=e^{x_{2}}$,解得 $a=e-1, b=e-e\ln(e-1)+\ln(e-1),x_{2}=\ln(e-1)$。因此,最佳一次逼近多项式为:
$$
P_{1}(x)=(e-1)x+e-e\ln(e-1)+\ln(e-1)
$$
# 习题 15
> 用 Schwarz 不等式估计 $\int_0^1 \dfrac{x^6}{1+x} \mathrm{d}x$ 的上界,并用积分中值定理估计同一积分的上、下界,并比较其结果。
# 习题 18
> $f(x)=|x|$ 在 $[-1,1]$ 上,求在 $\varphi_{1}=\mathbf{span}\{ 1,x^2,x^4 \}$ 上的最佳平方逼近
# 习题 21
> 把 $f(x)=\text{arccos}x$ 在 $[-1,1]$ 上展开成 Chebyshev 级数
# 习题 23
> 观察物体的直线运动,得出时间 $t$ 与距离 $s$ 的关系如下表所示,求运动方程
| $t / s$ | $0$ | $0.9$ | $1.9$ | $3.0$ | $3.9$ | $5.0$ |
| ------- | --- | ----- | ----- | ----- | ----- | ----- |
| $s / m$ | $0$ | $10$ | $30$ | $50$ | $80$ | $110$ |
''',
"[连接](https://www.baidu.com)\n`inline code`\n\n---\n\n- [ ] todo\n - [x] done\n - [-] doing\n\n---\n\n```python\nprint('abc')\n```\n另一端代码\n```js\nconsole.log(\"def\")\n```\n再看看 bash\n```bash\necho xxx\n```\n```json\n[{\"name\": \"bash\", \"code\": \"git status --porcelain\"}, {\"name\": \"bash\", \"code\": \"git diff HEAD\"}]\n```\n```c\nprintf(\"hello world\");\n```\n```cpp\nprintf(\"hello world\");\n```\n",
"""\n\n要证明命题 \\(\\neg\\neg\\neg\\neg p \\to p\\),可以通过以下步骤(基于经典逻辑中的双重否定律):\n\n### 证明过程:\n1. **假设前提**: \n 假设 \\(\\neg\\neg\\neg\\neg p\\) 成立。\n\n2. **第一次双重否定消除**: \n \\(\\neg\\neg\\neg\\neg p\\) 等价于 \\(\\neg\\neg(\\neg\\neg p)\\)。根据双重否定律(\\(\\neg\\neg q \\equiv q\\)),有:\n \\[\n \\neg\\neg(\\neg\\neg p) \\implies \\neg\\neg p.\n \\]\n\n3. **第二次双重否定消除**: \n 再次应用双重否定律,\\(\\neg\\neg p\\) 等价于 \\(p\\),因此:\n \\[\n \\neg\\neg p \\implies p.\n \\]\n\n4. **导出结论**: \n 通过两次双重否定消除,从 \\(\\neg\\neg\\neg\\neg p\\) 推导出 \\(p\\),即:\n \\[\n \\neg\\neg\\neg\\neg p \\implies p.\n \\]\n\n### 逻辑等价性解释:\n四次否定 \\(\\neg\\neg\\neg\\neg p\\) 可分解为两次双重否定:\n- 内层双重否定:\\(\\neg\\neg p \\equiv p\\)。\n- 外层双重否定:\\(\\neg\\neg(\\neg\\neg p) \\equiv \\neg\\neg p \\equiv p\\)。\n\n因此,\\(\\neg\\neg\\neg\\neg p \\to p\\) 等价于 \\(p \\to p\\),显然是永真式。\n\n### 结论:\n在经典逻辑中,\\(\\neg\\neg\\neg\\neg p \\to p\\) 成立。
"""
]
def gen(id:int):
global tt
ct = str(tt[id])
while len(ct) > 0:
time.sleep(0.05*random.random())
l = random.randint(8, 18)
if l > len(ct):
yield Chunk(ct)
break
else:
nt, ct = ct[:l], ct[l:]
yield Chunk(nt)
def parse():
import re
import json
import traceback
s = '\n\n```json\n[{\"name\": \"bash\", \"code\": \"git status --porcelain\"}, {\"name\": \"bash\", \"code\": \"git diff HEAD\"}]\n```'
commands = []
s = re.search(r'^([\s\S]*?)```(.*?)\n([\s\S]*)\n```([\s\S]*?)$',
s.strip(), re.S)
if s is None:
return None
s = s.groups()
if len(s) == 4 and s[1] == 'json':
try:
cmd = json.loads(s[2])
if type(cmd) is not list:
raise Exception("Invalid type")
for c in cmd:
if type(c) is not dict:
raise Exception("Invalid type")
if "name" not in c:
raise Exception("Invalid format")
if c['name'] not in ['python', 'bash']:
raise Exception("Invalid name")
commands.append(c)
except Exception as _:
traceback.print_exc()
print(commands)
return commands if len(commands) > 0 else None
def raw(id:int):
import rich
from rich.markdown import Markdown
from rich.console import Console
console = Console()
md = Markdown(tt[id])
console.print(md)
# print(md.parsed)
def main(id:int):
chat = Chat('', '')
chat._render_response(gen(id=id), 0)
# print(md.parsed)
# rich.inspect(console=console, obj=md.parsed)
if __name__ == '__main__':
sid = 8
raw(sid)
# main(sid)
# parse()