linzhaoxin319319 2 долоо хоног өмнө
parent
commit
35aee75609
1 өөрчлөгдсөн 356 нэмэгдсэн , 0 устгасан
  1. 356 0
      林兆新/stepbystep.ipynb

+ 356 - 0
林兆新/stepbystep.ipynb

@@ -0,0 +1,356 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "#步骤1 获取对应文件全部信息\n",
+    "from openai import OpenAI\n",
+    "from dotenv import load_dotenv \n",
+    "import os\n",
+    "import glob\n",
+    "import json\n",
+    "import pandas as pd\n",
+    "# print(\"✅ pandas 导入成功\")\n",
+    "\n",
+    "client = OpenAI(base_url=os.getenv(\"BAILIAN_API_BASE_URL\"),\n",
+    "       api_key=os.getenv(\"BAILIAN_API_KEY\"))\n"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "#步骤2\n",
+    "from datetime import datetime\n",
+    "\n",
+    "# 查询当前时间的工具。返回结果示例:“当前时间:2024-04-15 17:15:18。“\n",
+    "magic_num_n=0\n",
+    "magic_num_p=0\n",
+    "\n",
+    "def current_emotion_negative(reason: str):\n",
+    "    \"\"\"\"\"\"\n",
+    "    # 获取当前日期和时间\n",
+    "    current_datetime = datetime.now()\n",
+    "    # 格式化当前日期和时间\n",
+    "    formatted_time = current_datetime.strftime('%Y-%m-%d %H:%M:%S'),\n",
+    "    \n",
+    "    # 返回格式化后的当前时间\n",
+    "    # return f\"当前时间:{formatted_time}。\"\n",
+    "    global magic_num_n\n",
+    "    magic_num_n = magic_num_n +1\n",
+    "    print('magic_num_nmagic_num_nmagic_num_n', magic_num_n)\n",
+    "    return magic_num_n\n",
+    "\n",
+    "# current_emotion_negative()\n",
+    "# print('current_emotion_negativecurrent_emotion_negativecurrent_emotion_negative', magic_num_n)\n",
+    "\n",
+    "def current_emotion_positive(reason: str):\n",
+    "    \"\"\"\"\"\"\n",
+    "    # 获取当前日期和时间\n",
+    "    current_datetime = datetime.now()\n",
+    "    # 格式化当前日期和时间\n",
+    "    formatted_time = current_datetime.strftime('%Y-%m-%d %H:%M:%S'),\n",
+    "    \n",
+    "    # 返回格式化后的当前时间\n",
+    "    # return f\"当前时间:{formatted_time}。\"\n",
+    "    global magic_num_p\n",
+    "    magic_num_p = magic_num_p +1\n",
+    "    print('magic_num_pmagic_num_pmagic_num_p', magic_num_p)\n",
+    "    return magic_num_p\n",
+    "# current_emotion_negative()\n",
+    "# print('current_emotion_negativecurrent_emotion_negativecurrent_emotion_negative', magic_num_n)\n"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "\n",
+    "#调用接口的函数\n",
+    "def generate_response(\n",
+    "                    # user_input, \n",
+    "                    model, \n",
+    "                    messages,\n",
+    "                    # tools,\n",
+    "                    # system_prompt\n",
+    "                    ):\n",
+    "    try:\n",
+    "        completion = client.chat.completions.create(\n",
+    "            model=model,  # 通过参数传递\n",
+    "            \n",
+    "        # model=\"qwen3-32b\",\n",
+    "        # messages=[\n",
+    "        #     {\"role\": \"system\", \"content\": system_prompt},\n",
+    "        #     {\"role\": \"user\", \"content\": user_input}\n",
+    "        # ]\n",
+    "            messages = messages,\n",
+    "            tools=[\n",
+    "                {\n",
+    "                    \"type\": \"function\",\n",
+    "                    \"function\": {\n",
+    "                                \"name\": \"current_emotion_negative\",\n",
+    "                                \"description\": \"当你判断负面情绪的时候非常有用。\",\n",
+    "                                \"parameters\":{\n",
+    "                                    \"reason\":{\n",
+    "                                        \"type\":\"string\",\n",
+    "                                        \"description\":\"Th reason of the emotion.\"\n",
+    "                                    }\n",
+    "                                }\n",
+    "                                }\n",
+    "                },\n",
+    "                {\n",
+    "                    \"type\": \"function\",\n",
+    "                    \"function\": {\n",
+    "                                \"name\": \"current_emotion_positive\",\n",
+    "                                \"description\": \"当你判断正面情绪的时候非常有用。\",\n",
+    "                                }\n",
+    "                },\n",
+    "                \n",
+    "            ],\n",
+    "            tool_choice=\"auto\",  # 模型必须从两个工具中选择\n",
+    "            # max_tokens=0,  # 关键:禁止生成任何文本\n",
+    "            # tool_choice={\"type\": \"function\", \"function\": {\"name\": \"current_emotion_negative\"}}, # 关键参数:强制调用指定工具\n",
+    "            extra_body={\"enable_thinking\": False},\n",
+    "            temperature=0.1,\n",
+    "        )\n",
+    "        # print('completioncompletioncompletion', completion)\n",
+    "        # return completion.choices[0].message.content\n",
+    "        return completion\n",
+    "    except Exception as e:\n",
+    "        print(f\"最终失败: {str(e)}\"),\n",
+    "    \n",
+    "    return \"调用失败\""
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "# 获取 data 下所有子文件夹中的 .txt 文件\n",
+    "txt_files = glob.glob('../data/acllmdb_sentiment_small/negative/*.txt', recursive=True)\n",
+    "\n",
+    "# 存储所有结果的列表\n",
+    "emotion_results = []\n",
+    "\n",
+    "# print(f\"{txt_files}\")\n",
+    "def find_text_in_target ():\n",
+    "    for index, file_path in enumerate(txt_files, start=0):\n",
+    "        print(f\"找到文件: {file_path}\")\n",
+    "        with open(file_path, 'r', encoding='utf-8') as f:\n",
+    "            # print(f\"内容片段:\\n{f.read(1000)}...\\n\"\n",
+    "            content = f.read(2000),  # 读取前2000个字符\n",
+    "            # print('txt_filestxt_filestxt_files', txt_files[index])\n",
+    "            messages = [\n",
+    "    {\"role\": \"system\", \"content\": \"\"\"\n",
+    "你是一个很有帮助的助手。根据用户的输入内容,按照以下规则选择合适的工具\n",
+    " - 如果用户提供的文字情感不是正面的,请调用 ‘current_emotion_negative’ 函数\n",
+    " - 如果用户提供的文字情感不是负面的,请调用 ‘current_emotion_positive’ 函数\n",
+    "请以友好的语气回答问题。\"\"\"},\n",
+    "]\n",
+    "            messages.append(\n",
+    "                {\n",
+    "                    \"role\":\"user\",\n",
+    "                    \"content\":f\"{content}\"\n",
+    "                }\n",
+    "            ),\n",
+    "            # 调用时传入全局变量\n",
+    "            response = generate_response(\n",
+    "            # user_input=\"你好!\",\n",
+    "            model=\"qwen3-30b-a3b\",\n",
+    "            # model=\"qwen3-32b\",\n",
+    "            messages = messages\n",
+    "    # system_prompt=GLOBAL_PROMPT\n",
+    ")\n",
+    "            # print('response',response), \n",
+    "            # .choices[0].finish_reason\n",
+    "            # if response.conte\n",
+    "            if response==\"调用失败\":\n",
+    "                print('调用失败')\n",
+    "            elif response.choices[0].finish_reason=='tool_calls':\n",
+    "                function_name = response.choices[0].message.tool_calls[0].function.name\n",
+    "                arguments_string = response.choices[0].message.tool_calls[0].function.arguments\n",
+    "                # print('arguments_string',arguments_string)\n",
+    "\n",
+    "                # 使用json模块解析参数字符串\n",
+    "                try:\n",
+    "                    arguments = json.loads(arguments_string)\n",
+    "                except json.JSONDecodeError as e:\n",
+    "                    print(f\"JSON解析错误: {e}\")\n",
+    "                    print(f\"错误位置: 字符 {e.pos}\")\n",
+    "                    print(f\"原始字符串: '{arguments_string}'\")\n",
+    "                    # 尝试修复常见的JSON问题\n",
+    "                    try:\n",
+    "                        # 如果字符串为空或只包含空白字符\n",
+    "                        if not arguments_string or arguments_string.strip() == '':\n",
+    "                            arguments = {}\n",
+    "                        else:\n",
+    "                            # 尝试清理字符串\n",
+    "                            cleaned_string = arguments_string.strip()\n",
+    "                            if not cleaned_string.startswith('{'):\n",
+    "                                cleaned_string = '{' + cleaned_string\n",
+    "                            if not cleaned_string.endswith('}'):\n",
+    "                                cleaned_string = cleaned_string + '}'\n",
+    "                            arguments = json.loads(cleaned_string)\n",
+    "                    except:\n",
+    "                        print(\"无法修复JSON,使用空字典\")\n",
+    "                        arguments = {}\n",
+    "\n",
+    "                # print('arguments',arguments)   \n",
+    "                # print('function_name',function_name)    \n",
+    "                global current_emotion_negative\n",
+    "                global current_emotion_positive\n",
+    "                global magic_num_p\n",
+    "                global magic_num_n\n",
+    "                function_mapper = {\n",
+    "                        # \"get_current_weather\": get_current_weather,\n",
+    "                            \"current_emotion_negative\": current_emotion_negative,\n",
+    "                            \"current_emotion_positive\": current_emotion_positive,\n",
+    "                        # \"current_emotion_negative_test\": current_emotion_negative_test,\n",
+    "                            }\n",
+    "                        # 获取函数实体\n",
+    "                function = function_mapper[function_name]\n",
+    "                \n",
+    "                # 处理函数调用参数\n",
+    "                if function_name == \"current_emotion_negative\":\n",
+    "                    # current_emotion_negative 需要 reason 参数\n",
+    "                    reason = arguments.get('reason', '') if arguments else ''\n",
+    "                    function_output = function(reason)\n",
+    "                elif function_name == \"current_emotion_positive\":\n",
+    "                    # current_emotion_negative 需要 reason 参数\n",
+    "                    reason = arguments.get('reason', '') if arguments else ''\n",
+    "                    function_output = function(reason)\n",
+    "                else:\n",
+    "                    # 其他函数的通用处理\n",
+    "                    if arguments == {}:\n",
+    "                        function_output = function()\n",
+    "                    else:\n",
+    "                        function_output = function(arguments)\n",
+    "                # 打印工具的输出\n",
+    "                # print(f\"工具函数输出:{function_output}\\n\")\n",
+    "                emotion_results.append({\n",
+    "                    'index': index,\n",
+    "                    'content': content,\n",
+    "                    'reason': arguments.get('reason', ''),\n",
+    "                    'function_name': function_name,\n",
+    "                    'file_path': file_path,\n",
+    "                })\n",
+    "                print(f\"当前一共检验了{index+1}段文字,负面情绪{magic_num_n},正面情绪{magic_num_p}: {arguments.get('reason','')}\")\n",
+    "            # print(f'第{index}段, content内容:{content}')\n",
+    "    # return res\n",
+    "    # global magic_num_n\n",
+    "    # print('magic_num_nmagic_num_nmagic_num_nmagic_num_n', magic_num_n)\n",
+    "    return\n",
+    "\n",
+    "find_text_in_target()   \n",
+    "# pandas\n"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "\n"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "\n"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "messages = [\n",
+    "    {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n",
+    "]\n",
+    "\n",
+    "# 调用时传入全局变量\n",
+    "response = generate_response(\n",
+    "    # user_input=\"你好!\",\n",
+    "    model=\"qwen3-30b-a3b\",\n",
+    "    # model=\"qwen3-32b\",\n",
+    "    messages = messages,\n",
+    "    \n",
+    "    # system_prompt=GLOBAL_PROMPT\n",
+    ")\n",
+    "# print('response',response)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "#调用\n",
+    "tools = [\n",
+    "    {\n",
+    "        \"type\": \"function\",\n",
+    "        \"function\": {\n",
+    "            \"name\": \"current_emotion_negative\",\n",
+    "            \"description\": \"当你判断负面情绪的时候非常有用。\",\n",
+    "        }\n",
+    "    },\n",
+    "    {\n",
+    "        \"type\": \"function\",\n",
+    "        \"function\": {\n",
+    "            \"name\": \"current_emotion_positive\",\n",
+    "            \"description\": \"当你判断正面情绪的时候非常有用。\",\n",
+    "        }\n",
+    "    },\n",
+    "]\n"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "df = pd.DataFrame(emotion_results)\n",
+    "print('emotion_results ',emotion_results)\n",
+    "df.to_csv('emotion_results.csv', index=False)\n",
+    "print(\"✅ 结果已保存到 emotion_results.csv\")"
+   ]
+  }
+ ],
+ "metadata": {
+  "kernelspec": {
+   "display_name": ".venv",
+   "language": "python",
+   "name": "python3"
+  },
+  "language_info": {
+   "codemirror_mode": {
+    "name": "ipython",
+    "version": 3
+   },
+   "file_extension": ".py",
+   "mimetype": "text/x-python",
+   "name": "python",
+   "nbconvert_exporter": "python",
+   "pygments_lexer": "ipython3",
+   "version": "3.11.13"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}