|
@@ -40,28 +40,6 @@
|
|
|
]
|
|
|
},
|
|
|
{
|
|
|
- "cell_type": "code",
|
|
|
- "execution_count": 24,
|
|
|
- "id": "fe3620f8",
|
|
|
- "metadata": {},
|
|
|
- "outputs": [],
|
|
|
- "source": [
|
|
|
- "# 用于function calling的实践\n",
|
|
|
- "def handle_positive_sentiment(reason, result):\n",
|
|
|
- " \n",
|
|
|
- " print(\"这是一个积极的评价!\")\n",
|
|
|
- " print(f\"判断原因: {reason}\")\n",
|
|
|
- " print(f\"完整结果: {result}\")\n",
|
|
|
- " \n",
|
|
|
- "def handle_negative_sentiment(reason, result):\n",
|
|
|
- " \n",
|
|
|
- " print(\"这是一个消极的评价!\")\n",
|
|
|
- " print(f\"判断原因: {reason}\")\n",
|
|
|
- " print(f\"完整结果: {result}\")\n",
|
|
|
- " "
|
|
|
- ]
|
|
|
- },
|
|
|
- {
|
|
|
"cell_type": "markdown",
|
|
|
"id": "f1fb34ca",
|
|
|
"metadata": {},
|
|
@@ -81,13 +59,16 @@
|
|
|
"text": [
|
|
|
"{\n",
|
|
|
" \"sentiment\": \"positive\",\n",
|
|
|
- " \"reason\": \"The text expresses gratitude and a positive emotion.\"\n",
|
|
|
+ " \"reason\": \"The review text contains words like '感谢' (thankful), which indicate a positive sentiment.\"\n",
|
|
|
"}\n"
|
|
|
]
|
|
|
}
|
|
|
],
|
|
|
"source": [
|
|
|
"#预测方法\n",
|
|
|
+ "from ast import List\n",
|
|
|
+ "\n",
|
|
|
+ "\n",
|
|
|
"def predict_sentiment(text,model=\"qwen3-30b-a3b\"):\n",
|
|
|
" # 定义提示词\n",
|
|
|
" system_message = \"\"\"You are a sentiment analysis expert. Please analyze the sentiment tendency of the provided review text.\"\"\"\n",
|
|
@@ -100,51 +81,6 @@
|
|
|
" - reason: The reason for the sentiment tendency\n",
|
|
|
" \"\"\"\n",
|
|
|
"\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",
|
|
|
" model=model , # 如果model参数为空则使用默认值\n",
|
|
@@ -152,23 +88,55 @@
|
|
|
" # 添加系统提示词\n",
|
|
|
" {\"role\": \"system\", \"content\": system_message},\n",
|
|
|
" # TODO few-shot/one-shot 来增强表现的实践\n",
|
|
|
- " # {\"role\":\"user\", \"content\": \"\"\"Once again Mr. Costner has dragged out a movie for far longer than necessary. \n",
|
|
|
- " # Aside from the terrific sea rescue sequences, of which there are very few I just did not care about any of the characters. \n",
|
|
|
- " # Most of us have ghosts in the closet, and Costner's character are realized early on, and then forgotten until much later, \n",
|
|
|
- " # by which time I did not care. The character we should really care about is a very cocky, overconfident Ashton Kutcher. \n",
|
|
|
- " # The problem is he comes off as kid who thinks he's better than anyone else around him and shows no signs of a cluttered closet.\n",
|
|
|
- " # His only obstacle appears to be winning over Costner. \n",
|
|
|
- " # Finally when we are well past the half way point of this stinker, Costner tells us all about Kutcher's ghosts. \n",
|
|
|
- " # We are told why Kutcher is driven to be the best with no prior inkling or foreshadowing. \n",
|
|
|
- " # No magic here, it was all I could do to keep from turning it off an hour in.\"\"\"},\n",
|
|
|
- " # {\"role\":\"assistant\", \"content\": \"\"},\n",
|
|
|
" {\"role\": \"user\", \"content\": user_message}\n",
|
|
|
" ],\n",
|
|
|
" # Qwen3模型通过enable_thinking参数控制思考过程(开源版默认True,商业版默认False)\n",
|
|
|
" # 使用Qwen3开源版模型时,若未启用流式输出,请将下行取消注释,否则会报错\n",
|
|
|
+ " # TODO 定义工具用以大模型调用\n",
|
|
|
+ " tools=[{\n",
|
|
|
+ " \"type\": \"function\",\n",
|
|
|
+ " \"function\": {\n",
|
|
|
+ " \"name\": \"handle_positive_sentiment\",\n",
|
|
|
+ " \"description\": \"When you determine that the sentiment of a piece of text is positive, please use this method.\",\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_negative_sentiment\",\n",
|
|
|
+ " \"description\": \"When you determine that the sentiment of a piece of text is negative, please use this method.\",\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",
|
|
|
" extra_body={\"enable_thinking\": False},\n",
|
|
|
- "\n",
|
|
|
- " tools=tools,\n",
|
|
|
" temperature=0.3,\n",
|
|
|
" response_format={\"type\": \"json_object\"} # 指定返回JSON格式\n",
|
|
|
" )\n",
|
|
@@ -353,10 +321,181 @@
|
|
|
]
|
|
|
},
|
|
|
{
|
|
|
+ "cell_type": "markdown",
|
|
|
+ "id": "837a4320",
|
|
|
+ "metadata": {},
|
|
|
+ "source": [
|
|
|
+ "**Function Calling的尝试**"
|
|
|
+ ]
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "cell_type": "code",
|
|
|
+ "execution_count": null,
|
|
|
+ "id": "292cee13",
|
|
|
+ "metadata": {},
|
|
|
+ "outputs": [],
|
|
|
+ "source": [
|
|
|
+ "# 用于function calling的实践\n",
|
|
|
+ "def handle_positive_sentiment(reason, result):\n",
|
|
|
+ " print(\"这是一个积极的评价!\")\n",
|
|
|
+ " print(f\"判断原因: {reason}\")\n",
|
|
|
+ " print(f\"完整结果: {result}\")\n",
|
|
|
+ " \n",
|
|
|
+ "def handle_negative_sentiment(reason, result):\n",
|
|
|
+ " print(\"这是一个消极的评价!\")\n",
|
|
|
+ " print(f\"判断原因: {reason}\")\n",
|
|
|
+ " print(f\"完整结果: {result}\")\n",
|
|
|
+ " "
|
|
|
+ ]
|
|
|
+ },
|
|
|
+ {
|
|
|
"cell_type": "code",
|
|
|
"execution_count": null,
|
|
|
"id": "47872e36",
|
|
|
"metadata": {},
|
|
|
+ "outputs": [
|
|
|
+ {
|
|
|
+ "name": "stdout",
|
|
|
+ "output_type": "stream",
|
|
|
+ "text": [
|
|
|
+ "ChatCompletion(id='chatcmpl-eda3227c-0060-9319-84f9-ff3701626208', choices=[Choice(finish_reason='tool_calls', index=0, logprobs=None, message=ChatCompletionMessage(content='', refusal=None, role='assistant', annotations=None, audio=None, function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_2fde3180722a4c0cb6d352', function=Function(arguments='{\"reason\": \"The review text \\'你好!我很感谢你\\' contains positive expressions such as \\'你好\\' (hello) and \\'很感谢你\\' (I am very grateful to you), which indicate a positive sentiment.\", \"result\": \"positive\"}', name='handle_positive_sentiment'), type='function', index=0)], reasoning_content=''))], created=1752227871, model='qwen3-4b', object='chat.completion', service_tier=None, system_fingerprint=None, usage=CompletionUsage(completion_tokens=66, prompt_tokens=468, total_tokens=534, completion_tokens_details=None, prompt_tokens_details=None))\n"
|
|
|
+ ]
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ "source": [
|
|
|
+ "def predict_sentiment_functioncalling(text,model=\"qwen3-4b\"):\n",
|
|
|
+ " # 定义提示词\n",
|
|
|
+ " system_message = \"\"\"You are a sentiment analysis expert. Please analyze the sentiment tendency of the provided review text.\"\"\"\n",
|
|
|
+ " user_message = f\"\"\"\n",
|
|
|
+ " Please analyze the sentiment tendency (positive or negative) of the following review text (provided within <>).\n",
|
|
|
+ " You can use the following tools to help you analyze the sentiment tendency:\n",
|
|
|
+ " - handle_positive_sentiment: When you determine that the sentiment of a piece of text is positive, please use this method.\n",
|
|
|
+ " - handle_negative_sentiment: When you determine that the sentiment of a piece of text is negative, please use this method.\n",
|
|
|
+ "\n",
|
|
|
+ " Review text: <{text}>\n",
|
|
|
+ " \"\"\"\n",
|
|
|
+ "\n",
|
|
|
+ " # 调用大模型进行情感分析\n",
|
|
|
+ " response = client.chat.completions.create(\n",
|
|
|
+ " model=model , # 如果model参数为空则使用默认值\n",
|
|
|
+ " # TODO 定义工具用以大模型调用\n",
|
|
|
+ " tools=[{\n",
|
|
|
+ " \"type\": \"function\",\n",
|
|
|
+ " \"function\": {\n",
|
|
|
+ " \"name\": \"handle_positive_sentiment\",\n",
|
|
|
+ " \"description\": \"When you determine that the sentiment of a piece of text is positive, please use this method.\",\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_negative_sentiment\",\n",
|
|
|
+ " \"description\": \"When you determine that the sentiment of a piece of text is negative, please use this method.\",\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",
|
|
|
+ " messages=[\n",
|
|
|
+ " # 添加系统提示词\n",
|
|
|
+ " {\"role\": \"system\", \"content\": system_message},\n",
|
|
|
+ " # TODO few-shot/one-shot 来增强表现的实践\n",
|
|
|
+ " {\"role\": \"user\", \"content\": user_message}\n",
|
|
|
+ " ],\n",
|
|
|
+ " # Qwen3模型通过enable_thinking参数控制思考过程(开源版默认True,商业版默认False)\n",
|
|
|
+ " # 使用Qwen3开源版模型时,若未启用流式输出,请将下行取消注释,否则会报错\n",
|
|
|
+ " extra_body={\"enable_thinking\": False},\n",
|
|
|
+ " tool_choice=\"auto\",\n",
|
|
|
+ " temperature=0.3,\n",
|
|
|
+ " #response_format={\"type\": \"json_object\"} # 指定返回JSON格式\n",
|
|
|
+ " )\n",
|
|
|
+ " # 获取返回结果\n",
|
|
|
+ " # result = response.choices[0].message\n",
|
|
|
+ " \n",
|
|
|
+ " return response\n",
|
|
|
+ "\n",
|
|
|
+ "#测试\n",
|
|
|
+ "completion=predict_sentiment_functioncalling(\"你好!我很感谢你\")\n",
|
|
|
+ "print(completion)"
|
|
|
+ ]
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "cell_type": "code",
|
|
|
+ "execution_count": null,
|
|
|
+ "id": "865f16a8",
|
|
|
+ "metadata": {},
|
|
|
+ "outputs": [
|
|
|
+ {
|
|
|
+ "name": "stdout",
|
|
|
+ "output_type": "stream",
|
|
|
+ "text": [
|
|
|
+ "这是一个积极的评价!\n",
|
|
|
+ "判断原因: The review text '你好!我很感谢你' contains positive expressions such as '你好' (hello) and '很感谢你' (I am very grateful to you), which indicate a positive sentiment.\n",
|
|
|
+ "完整结果: positive\n",
|
|
|
+ "工具函数输出:None\n",
|
|
|
+ "\n"
|
|
|
+ ]
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ "source": [
|
|
|
+ "#执行工具函数\n",
|
|
|
+ "# 从返回的结果中获取函数名称和入参\n",
|
|
|
+ "if completion.choices[0].message.tool_calls:\n",
|
|
|
+ " function_name = completion.choices[0].message.tool_calls[0].function.name\n",
|
|
|
+ " arguments_string = completion.choices[0].message.tool_calls[0].function.arguments\n",
|
|
|
+ "\n",
|
|
|
+ " # 使用json模块解析参数字符串\n",
|
|
|
+ " arguments = json.loads(arguments_string)\n",
|
|
|
+ " # 创建一个函数映射表\n",
|
|
|
+ " function_mapper = {\n",
|
|
|
+ " \"handle_positive_sentiment\": handle_positive_sentiment,\n",
|
|
|
+ " \"handle_negative_sentiment\": handle_negative_sentiment\n",
|
|
|
+ " }\n",
|
|
|
+ " # 获取函数实体\n",
|
|
|
+ " function = function_mapper[function_name]\n",
|
|
|
+ " # 如果入参为空,则直接调用函数\n",
|
|
|
+ " if arguments == {}:\n",
|
|
|
+ " arguments = None\n",
|
|
|
+ " # 否则,传入参数后调用函数\n",
|
|
|
+ " else:\n",
|
|
|
+ " function_output = function(**arguments)\n",
|
|
|
+ " \n",
|
|
|
+ "else:\n",
|
|
|
+ " function_name = None\n",
|
|
|
+ " arguments_string = None\n"
|
|
|
+ ]
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "cell_type": "code",
|
|
|
+ "execution_count": null,
|
|
|
+ "id": "85dc78a7",
|
|
|
+ "metadata": {},
|
|
|
"outputs": [],
|
|
|
"source": []
|
|
|
}
|