|
@@ -10,7 +10,7 @@
|
|
|
},
|
|
|
{
|
|
|
"cell_type": "code",
|
|
|
- "execution_count": null,
|
|
|
+ "execution_count": 23,
|
|
|
"id": "ca680f71",
|
|
|
"metadata": {},
|
|
|
"outputs": [],
|
|
@@ -41,28 +41,20 @@
|
|
|
},
|
|
|
{
|
|
|
"cell_type": "code",
|
|
|
- "execution_count": null,
|
|
|
+ "execution_count": 24,
|
|
|
"id": "fe3620f8",
|
|
|
"metadata": {},
|
|
|
"outputs": [],
|
|
|
"source": [
|
|
|
"# 用于function calling的实践\n",
|
|
|
"def handle_positive_sentiment(reason, result):\n",
|
|
|
- " \"\"\"处理积极情感的方法\n",
|
|
|
- " Args:\n",
|
|
|
- " reason: 大模型给出的积极判断原因\n",
|
|
|
- " result: 大模型返回的完整结果\n",
|
|
|
- " \"\"\"\n",
|
|
|
+ " \n",
|
|
|
" print(\"这是一个积极的评价!\")\n",
|
|
|
" print(f\"判断原因: {reason}\")\n",
|
|
|
" print(f\"完整结果: {result}\")\n",
|
|
|
" \n",
|
|
|
"def handle_negative_sentiment(reason, result):\n",
|
|
|
- " \"\"\"处理消极情感的方法\n",
|
|
|
- " Args:\n",
|
|
|
- " reason: 大模型给出的消极判断原因\n",
|
|
|
- " result: 大模型返回的完整结果\n",
|
|
|
- " \"\"\" \n",
|
|
|
+ " \n",
|
|
|
" print(\"这是一个消极的评价!\")\n",
|
|
|
" print(f\"判断原因: {reason}\")\n",
|
|
|
" print(f\"完整结果: {result}\")\n",
|
|
@@ -82,7 +74,18 @@
|
|
|
"execution_count": null,
|
|
|
"id": "e874b885",
|
|
|
"metadata": {},
|
|
|
- "outputs": [],
|
|
|
+ "outputs": [
|
|
|
+ {
|
|
|
+ "name": "stdout",
|
|
|
+ "output_type": "stream",
|
|
|
+ "text": [
|
|
|
+ "{\n",
|
|
|
+ " \"sentiment\": \"positive\",\n",
|
|
|
+ " \"reason\": \"The text expresses gratitude and a positive emotion.\"\n",
|
|
|
+ "}\n"
|
|
|
+ ]
|
|
|
+ }
|
|
|
+ ],
|
|
|
"source": [
|
|
|
"#预测方法\n",
|
|
|
"def predict_sentiment(text,model=\"qwen3-30b-a3b\"):\n",
|
|
@@ -97,8 +100,50 @@
|
|
|
" - reason: The reason for the sentiment tendency\n",
|
|
|
" \"\"\"\n",
|
|
|
"\n",
|
|
|
- " # 定义工具用以大模型调用\n",
|
|
|
- " tools=[]\n",
|
|
|
+ " # TODO 定义工具用以大模型调用\n",
|
|
|
+ " tools=[{\n",
|
|
|
+ " \"type\": \"function\",\n",
|
|
|
+ " \"function\": {\n",
|
|
|
+ " \"name\": \"handle_positive_sentiment\",\n",
|
|
|
+ " \"description\": \"当你判断一段文本的情感倾向为积极positive的时候,请使用这个方法\",\n",
|
|
|
+ " \"parameters\": {\n",
|
|
|
+ " \"type\": \"object\",\n",
|
|
|
+ " \"properties\": {\n",
|
|
|
+ " \"reason\": {\n",
|
|
|
+ " \"type\": \"string\",\n",
|
|
|
+ " \"description\": \"你判断情感倾向为积极的原因\"\n",
|
|
|
+ " },\n",
|
|
|
+ " \"result\":{\n",
|
|
|
+ " \"type\": \"string\",\n",
|
|
|
+ " \"description\": \"你判断情感倾向为积极的结果\",\n",
|
|
|
+ " \"enum\": [\"positive\", \"negative\"]\n",
|
|
|
+ " }\n",
|
|
|
+ " },\n",
|
|
|
+ " \"required\": [\"reason\", \"result\"],\n",
|
|
|
+ " }\n",
|
|
|
+ " }\n",
|
|
|
+ " },{\n",
|
|
|
+ " \"type\": \"function\",\n",
|
|
|
+ " \"function\": {\n",
|
|
|
+ " \"name\": \"handle_positive_sentiment\",\n",
|
|
|
+ " \"description\": \"当你判断一段文本的情感倾向为消极negative的时候,请使用这个方法\",\n",
|
|
|
+ " \"parameters\": {\n",
|
|
|
+ " \"type\": \"object\",\n",
|
|
|
+ " \"properties\": {\n",
|
|
|
+ " \"reason\": {\n",
|
|
|
+ " \"type\": \"string\",\n",
|
|
|
+ " \"description\": \"你判断情感倾向为积极的原因\"\n",
|
|
|
+ " },\n",
|
|
|
+ " \"result\":{\n",
|
|
|
+ " \"type\": \"string\",\n",
|
|
|
+ " \"description\": \"你判断情感倾向为积极的结果\",\n",
|
|
|
+ " \"enum\": [\"positive\", \"negative\"]\n",
|
|
|
+ " }\n",
|
|
|
+ " },\n",
|
|
|
+ " \"required\": [\"reason\", \"result\"],\n",
|
|
|
+ " }\n",
|
|
|
+ " }\n",
|
|
|
+ " }]\n",
|
|
|
" \n",
|
|
|
" # 调用大模型进行情感分析\n",
|
|
|
" response = client.chat.completions.create(\n",
|
|
@@ -123,11 +168,10 @@
|
|
|
" # 使用Qwen3开源版模型时,若未启用流式输出,请将下行取消注释,否则会报错\n",
|
|
|
" extra_body={\"enable_thinking\": False},\n",
|
|
|
"\n",
|
|
|
- " #tools=tools,\n",
|
|
|
+ " tools=tools,\n",
|
|
|
" temperature=0.3,\n",
|
|
|
" response_format={\"type\": \"json_object\"} # 指定返回JSON格式\n",
|
|
|
" )\n",
|
|
|
- " \n",
|
|
|
" # 获取返回结果\n",
|
|
|
" result = response.choices[0].message.content\n",
|
|
|
" return result\n",
|