Update README.md
Browse files
README.md
CHANGED
|
@@ -127,58 +127,105 @@ Save a legible drawing as `test.jpg` in your working directory, then run the fol
|
|
| 127 |
```python
|
| 128 |
import base64
|
| 129 |
from openai import OpenAI
|
|
|
|
| 130 |
|
| 131 |
client = OpenAI(api_key="EMPTY", base_url="http://localhost:18124/v1")
|
| 132 |
model = "LevelField"
|
| 133 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
def encode_image_base64(image_path):
|
| 136 |
with open(image_path, "rb") as f:
|
| 137 |
return base64.b64encode(f.read()).decode("utf-8")
|
| 138 |
|
| 139 |
|
| 140 |
-
image_path = "test.
|
| 141 |
image_base64 = encode_image_base64(image_path)
|
| 142 |
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
model=model,
|
| 145 |
messages=[
|
|
|
|
| 146 |
{
|
| 147 |
"role": "user",
|
| 148 |
"content": [
|
| 149 |
-
{
|
| 150 |
-
"type": "text",
|
| 151 |
-
"text": (
|
| 152 |
-
"您是一位工艺工程师。现在给您加工图纸<image>。"
|
| 153 |
-
"请根据可供选择的工艺`车削,铣削,钻削,磨削,铸造,锻造,"
|
| 154 |
-
"热处理,表面处理,电火花加工,钳加工,装配,齿加工,"
|
| 155 |
-
"钣金成型,焊接,线切割,激光打标,检验,下料`,"
|
| 156 |
-
"给出它的工艺序列。"
|
| 157 |
-
),
|
| 158 |
-
},
|
| 159 |
{
|
| 160 |
"type": "image_url",
|
| 161 |
-
"image_url": {
|
| 162 |
-
"url": f"data:image/jpeg;base64,{image_base64}"
|
| 163 |
-
},
|
| 164 |
},
|
| 165 |
],
|
| 166 |
-
}
|
| 167 |
],
|
| 168 |
-
temperature=0.
|
| 169 |
-
|
|
|
|
|
|
|
|
|
|
| 170 |
extra_body={
|
| 171 |
-
"
|
|
|
|
| 172 |
},
|
| 173 |
)
|
| 174 |
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
```
|
| 183 |
|
| 184 |
This example enables thinking through `chat_template_kwargs` and prints the reasoning field when returned, followed by the final answer. Use it with the server configuration above, including `--reasoning-parser qwen3`.
|
|
|
|
| 127 |
```python
|
| 128 |
import base64
|
| 129 |
from openai import OpenAI
|
| 130 |
+
import json
|
| 131 |
|
| 132 |
client = OpenAI(api_key="EMPTY", base_url="http://localhost:18124/v1")
|
| 133 |
model = "LevelField"
|
| 134 |
|
| 135 |
+
SYSTEM_PROMPT = (
|
| 136 |
+
"你是一位机械制造工艺设计专家。"
|
| 137 |
+
"请根据工程图纸,先从图纸中提取零件信息(标题栏、技术要求等),"
|
| 138 |
+
"然后推导出完整的工艺流程。请用中文输出。"
|
| 139 |
+
"如果图纸不清晰、不是机械工程图纸、或无法识别关键信息,请说明原因,不要编造内容。"
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
PROCESSES = [
|
| 143 |
+
"落料",
|
| 144 |
+
"普车", "数车", "小普车",
|
| 145 |
+
"普铣", "三轴", "四轴", "五轴", "卧加",
|
| 146 |
+
"平面磨", "内外圆磨", "外圆磨", "平磨", "磨床",
|
| 147 |
+
"中丝", "慢丝", "快丝", "线割",
|
| 148 |
+
"穿孔", "电火花", "深孔", "深孔钻", "摇臂钻", "钻床",
|
| 149 |
+
"热处理", "回火", "表面处理", "抛光",
|
| 150 |
+
"检验", "三坐标", "钳工",
|
| 151 |
+
"装配", "压配", "铜焊", "焊接", "热配",
|
| 152 |
+
"试压", "道序委外", "流转", "动平衡", "激光雕刻",
|
| 153 |
+
]
|
| 154 |
+
|
| 155 |
+
EQUIPMENT_MAP = {}
|
| 156 |
+
|
| 157 |
|
| 158 |
def encode_image_base64(image_path):
|
| 159 |
with open(image_path, "rb") as f:
|
| 160 |
return base64.b64encode(f.read()).decode("utf-8")
|
| 161 |
|
| 162 |
|
| 163 |
+
image_path = "test.png"
|
| 164 |
image_base64 = encode_image_base64(image_path)
|
| 165 |
|
| 166 |
+
user_prompt = f"""【可用工序词表】
|
| 167 |
+
{json.dumps(PROCESSES, ensure_ascii=False, indent=2)}
|
| 168 |
+
|
| 169 |
+
【可用设备清单】
|
| 170 |
+
{{}}
|
| 171 |
+
|
| 172 |
+
【可用刀具清单】
|
| 173 |
+
{{}}
|
| 174 |
+
|
| 175 |
+
【可用检具清单】
|
| 176 |
+
{{}}
|
| 177 |
+
|
| 178 |
+
请分析这张工程图纸,根据上述工序词表、设备清单、刀具清单、检具清单和工艺规则,推导工艺流程并输出JSON。
|
| 179 |
+
|
| 180 |
+
要求:
|
| 181 |
+
1. 工序名称只能使用工序词表中的名称
|
| 182 |
+
2. 设备只能从可用设备清单中选择(多台设备用分号分隔),清单中无对应工序时留空
|
| 183 |
+
3. 刀具优先从刀具清单中选择,清单为空时根据工艺常识推导填写
|
| 184 |
+
4. 检具优先从检具清单中选择,清单为空时根据工艺常识推导填写
|
| 185 |
+
5. JSON中不要使用LaTeX格式,请使用Unicode字符(如φ)"""
|
| 186 |
+
|
| 187 |
+
stream = client.chat.completions.create(
|
| 188 |
model=model,
|
| 189 |
messages=[
|
| 190 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
| 191 |
{
|
| 192 |
"role": "user",
|
| 193 |
"content": [
|
| 194 |
+
{"type": "text", "text": user_prompt},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
{
|
| 196 |
"type": "image_url",
|
| 197 |
+
"image_url": {"url": f"data:image/{'png' if image_path.lower().endswith('.png') else 'jpeg'};base64,{image_base64}"},
|
|
|
|
|
|
|
| 198 |
},
|
| 199 |
],
|
| 200 |
+
},
|
| 201 |
],
|
| 202 |
+
temperature=0.85,
|
| 203 |
+
top_p=0.95,
|
| 204 |
+
max_tokens=16384,
|
| 205 |
+
presence_penalty=1.0,
|
| 206 |
+
stream=True,
|
| 207 |
extra_body={
|
| 208 |
+
"top_k": 20,
|
| 209 |
+
"chat_template_kwargs": {"enable_thinking": True},
|
| 210 |
},
|
| 211 |
)
|
| 212 |
|
| 213 |
+
print("【思考过程】", flush=True)
|
| 214 |
+
in_reasoning = True
|
| 215 |
+
for chunk in stream:
|
| 216 |
+
if not chunk.choices:
|
| 217 |
+
continue
|
| 218 |
+
delta = chunk.choices[0].delta
|
| 219 |
+
reasoning = getattr(delta, "reasoning", None) # 思考内容
|
| 220 |
+
content = getattr(delta, "content", None) # 最终回答
|
| 221 |
+
if reasoning:
|
| 222 |
+
print(reasoning, end="", flush=True)
|
| 223 |
+
if content:
|
| 224 |
+
if in_reasoning: # 思考结束, 切到回答
|
| 225 |
+
print("\n\n【最终回答】", flush=True)
|
| 226 |
+
in_reasoning = False
|
| 227 |
+
print(content, end="", flush=True)
|
| 228 |
+
print()
|
| 229 |
```
|
| 230 |
|
| 231 |
This example enables thinking through `chat_template_kwargs` and prints the reasoning field when returned, followed by the final answer. Use it with the server configuration above, including `--reasoning-parser qwen3`.
|