dagent-llm


Namedagent-llm JSON
Version 0.2.0 PyPI version JSON
download
home_pagehttps://gitee.com/iint/dagent_llm
SummaryA package for LLM operations.
upload_time2024-10-20 03:03:06
maintainerNone
docs_urlNone
authorZhao Sheng
requires_python>=3.10
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <p align="center">
  <img src="dagent_llm.png" alt="DCheck Logo" width="250">
</p>

# DAgent: Command Line Interface for Language Model Operations
[中文](README_zh.md) | English
## Overview
The LLM Package is a Python-based command-line interface (CLI) that provides an easy way to interact with Large Language Models (LLMs). It allows users to chat with the model, make choices based on given options, and more. This package is designed to be simple, intuitive, and extendable for various LLM operations.
## Features
- Chat with the LLM and receive responses.
- Present options to the LLM and get a choice.
- Choose an option and provide arguments for further processing.
- Few-shot learning capabilities for better context understanding.
- Logging of conversation history for future reference.
## Installation
To install the LLM Package, run the following command:
```bash
pip install dagent_llm
```
Ensure that you have Python 3.6 or later installed on your system.
## Help
To view the available commands and options, use the `help` flag:
```bash
dagent_llm help
```
This will display the list of available commands and their descriptions.
> Note : Dagent_llm assumes that you have configured the environment variables through [dsqlenv](https://pypi.org/project/dsqlenv/), and the system will read the necessary information from [dsqlenv](https://pypi.org/project/dsqlenv/).
```
D-Agent LLM Command Line Interface

Usage: dagent_llm 

Available Commands:
  chat             Send a message to the LLM and get a response.
  choose           Present options to the LLM and get a choice.
  choose_with_args Choose an option and provide arguments.

Options for 'chat' command:
  --message         The message to send to the LLM.
  --llm_server      Specify the LLM server to use.
  --role            Specify the role of the message sender (default: 'human').

Options for 'choose' command:
  --options         List of options to choose from.
  --prompt          The prompt for choosing.
  --need-reason     Ask the LLM to provide reasons for the choice.
  --multiple        Allow the LLM to select multiple options.
  --notes           Additional notes to add to the prompt.
  --examples        Few-shot learning examples to guide the choice.

Options for 'choose_with_args' command:
  --options         List of options to choose from.
  --prompt          The prompt for choosing.
  --option-type     The type of options being chosen.
  --need-reason     Provide reasons for the choice.
  --multiple        Allow multiple selections.
  --notes           Additional notes to add to the prompt.
  --examples        Few-shot learning examples to guide the choice.

Version: 0.1.0 | 2024-10-18
Copyright: © 2024 VoiceCodeAI, Singapore
```

## Dependencies
- Python 3.6+
- dsqlenv
- langchain_core
- langchain_openai

## Usage
### Chatting with the LLM
To send a message to the LLM and receive a response, use the `chat` command:
```bash
dagent_llm chat --message "Hello, how are you?" --role human
```
The `--role` flag can be set to `human`, `ai`, or `system` depending on the context of the message.
### Making a Choice
To present options to the LLM and get a choice, use the `choose` command:
```bash
dagent_llm choose --options "Option 1" "Option 2" "Option 3" --prompt "Choose an option" --need-reason --multiple
```
The `--need-reason` flag will ask the LLM to provide reasons for the choice, and the `--multiple` flag allows the selection of multiple options.
### Choosing with Arguments
To choose an option and provide arguments, use the `choose_with_args` command:
```bash
dagent_llm choose_with_args --options "Option 1" "Option 2" "Option 3" --prompt "Choose an option and provide arguments" --option-type "type" --need-reason --multiple
```
The `--option-type` flag describes the type of options being chosen.
### Providing Few-Shot Examples
You can provide few-shot examples to guide the LLM using the `examples` argument:
```bash
dagent_llm choose --options ... --prompt ... --examples "Example 1" "Example 2"
```
### Adding Notes
Additional notes can be added to the prompt using the `notes` argument:
```bash
dagent_llm choose --options ... --prompt ... --notes "Note 1" "Note 2"
```
## Demo
Here's a simple demo to demonstrate chatting with the LLM:
```bash
# Chat with the LLM
dagent_llm chat --message "What's the weather like today?" --role human
# Output:
# LLM response: The weather is sunny with a few clouds.
```

## Python API
The LLM Package can also be used as a Python library. Here's an example of how to chat with the LLM using the Python API:
```python
from dagent_llm import LLM
from langchain_core.messages import HumanMessage, SystemMessage, AIMessage

# Initialize the LLM model with a specific engine
model = LLM("deepseek")  # 'deepseek' is the engine being used for LLM
# Note: Directly starting with llm_server requires you to have installed dsqlenv and configured it.
# For example: {llm_server}_api_key, {llm_server}_base_url, {llm_server}_model, etc.
# Please refer to [dsqlenv](https://pypi.org/project/dsqlenv/) for specific configuration methods.

# Or you can specify the complete engine information
model = LLM(
    llm_server="deepseek",
    max_retries=3, # Max retry times
    ###################################
    # OpenAI API parameters
    temperature=0.7,
    api_key="your_api_key",
    base_url="https://api.deepseek.com",
    model_name="deepseek-chat", 
    ###################################
    history=[] # Same as langchain_core.messages
)


# Simple interaction with the model
r = model.chat("Tell me about yourself")
print(r.content)  # Outputs the response from the model

# Define a list of functions with their names and inputs
functions_info = [
    {"name": "get_data", "input": ["id"], "example_input": "a"},
    {"name": "insert_data", "input": ["id", "data"], "example_input": ["a", "b"]},
    {"name": "update_data", "input": ["id", "data"], "example_input": ["a", "b"]},
    {"name": "delete_data", "input": ["id"], "example_input": "a"},
]

# Example 1: Selecting a function based on user input, including reasons for choice
# Here, the model will be asked to select a function and provide the necessary arguments.
r = model.function_choose(
    functions_info,                      # List of functions to choose from
    "Add a record with key-value pair abc and 123",  # The prompt asking what to do
    need_reason=True,                    # Model must provide a reason for its choice
    multiple=False,                      # Single function selection allowed
    add_to_history=True                  # Add this interaction to the conversation history
)
print(r)  # Outputs the selected function and arguments


# Example 2: Function selection with additional context such as examples and notes
# This provides the model with extra guidance on how to make its decision
r2 = model.function_choose(
    functions_info,
    "Delete record with key abc",        # Instruction for deletion operation
    need_reason=True,                    # Model must provide reasoning
    multiple=False,                      # Only one function can be selected
    add_to_history=True,                 # Record this interaction
    examples=[                           # Example to guide the model
        "Add a record with key-value pair abc and 123 -> insert_data('abc', '123')"
    ],
    notes=[                              # Important notes for the operation
        "Delete operation is irreversible",  
        "This will delete all records with key 'abc'"
    ]
)
print(r2)  # Outputs the selected function and explanation


# Example 3: Simple selection scenario for choosing from a list of food options
# Multiple selections are allowed in this case, and the model needs to justify its choice
foods = ["Snail noodles", "Rice noodles", "Beef noodles", "Egg noodles", "Vegetable salad", "Boiled beef"]
r = model.choose(
    foods,                               # List of options to choose from
    "What can I eat while on a diet?",   # The question or prompt
    "Food name",                         # Type of options being chosen
    need_reason=True,                    # Model must provide reasons for its choices
    multiple=True,                       # Multiple choices allowed (diet-friendly foods)
    add_to_history=True                  # Record the conversation
)
print(r)  # Outputs the selected food(s) and reason(s)

# Review conversation history to see how previous interactions were logged
print(model.history)

# If token information is needed (optional debugging for developers):
# print(model.input_tokens)
```
## APP: Telephone Customer Service Quality Inspection System: Combining `dagent_llm`, `dguard`, and `dspeech`

This demo showcases how to build a quality inspection system for telephone customer service using the following components:
- **`dagent_llm`**: A large language model (LLM) used for evaluating the dialogue content, identifying emotions, solving user problems, and ensuring compliance with customer service standards.
- **`dguard`**: A diarization model used to identify speakers and segment audio files by speaker turns.
- **`dspeech`**: A speech-to-text (STT) model used for transcribing audio content and classifying emotions.

### Demo Features
This system processes recorded customer service calls, providing:
1. **Speaker diarization**: Identifies different speakers from the audio.
2. **Emotion analysis**: Assesses emotions for both customer service agents and customers.
3. **Service quality evaluation**: Determines whether customer problems are solved and evaluates if the agent followed proper procedures.

### Requirements
- Python 3.8+
- `dagent_llm`, `dguard`, and `dspeech` installed
- Additional Python libraries: `rich`, `os`, `csv`, `subprocess`

### System Workflow
1. **Input WAV Files**: The system takes in audio files (WAV format) from the customer service call recordings.
2. **Audio Preprocessing**: The audio is downsampled to a single channel (16 kHz) using `ffmpeg`.
3. **Speaker Diarization**: The `dguard` model identifies different speakers in the audio and segments it based on speaker turns.
4. **Speech Transcription**: The `dspeech` model transcribes each speaker’s segment into text.
5. **Emotion Classification**: For segments longer than a set threshold (e.g., 2 seconds), the system classifies emotions using the `dspeech` model.
6. **Dialogue Evaluation**: The system uses `dagent_llm` to assess:
   - Agent's emotions: Whether they exhibit negative emotions.
   - Customer's emotions: Whether they are satisfied or dissatisfied with the service.
   - Problem resolution: Whether the customer’s issue was solved.
   - Procedural compliance: Whether the agent followed proper service procedures.
7. **Results**: The system outputs a CSV file summarizing the evaluation results, along with individual text files for each conversation.

### Code Overview

Below is a breakdown of the main functions and their roles:

#### 1. **rich_print**
This function uses the `rich` library to color-code and format output. It highlights speaker turns and emotions.

```python
def rich_print(content):
    colors = ["red", "green", "blue", "yellow", "magenta", "cyan"]
    for idx, line in enumerate(content.split("\n")):
        if ":" not in line:
            continue
        spk_id = line.split(":")[0].split(" ")[-1]
        console.print(f"[{colors[int(spk_id) % 6]}]{line}")
```

#### 2. **get_diarization_content**
This function processes a WAV file to generate speaker-diarized transcriptions. For each speaker, it transcribes the speech and classifies emotions if the speaking duration exceeds the `emotion_time_threshold`.

```python
def get_diarization_content(file_path, emotion_time_threshold=2):
    try:
        r = dm_model.diarize(file_path)
        all_content = ""
        last_spk = ""
        for data in r:
            spk_label = data[3]
            start_time = data[1]
            end_time = data[2]
            generate_text = stt_model.transcribe_file(file_path, start=start_time, end=end_time)
            if end_time - start_time > emotion_time_threshold:
                emotion = stt_model.emo_classify_file(file_path, start=start_time, end=end_time)
                emotion_label = emotion["labels"][emotion["scores"].index(max(emotion["scores"]))]
                emotion_score = max(emotion["scores"])
                emotion_text = f"(emotion:{emotion_label} with score: {emotion_score:.2f})"
            else:
                emotion_text = ""
            if spk_label != last_spk:
                all_content += f"\nSpeaker {spk_label}: {generate_text} " + emotion_text
                last_spk = spk_label
            else:
                all_content += f" {generate_text}"
        return all_content
    except Exception as e:
        console.print(f"[red]Error processing file {file_path}: {str(e)}[/red]")
        return ""
```

#### 3. **evaluate_wav_file**
This function evaluates the quality of the customer service conversation using the `dagent_llm`. It provides:
- Agent emotion evaluation
- Customer satisfaction
- Problem resolution assessment
- Procedural compliance evaluation

```python
def evaluate_wav_file(content):
    try:
        chooses = ["符合要求(无负面情绪)", "不符合要求(有负面情绪)"]
        prompt = f"<对话内容>\n{content}<对话内容>\n请你根据对话内容评估客服人员的情绪是否符合要求..."
        r = dagent_llm.choose(chooses, prompt, "情绪是否符合要求", need_reason=True)
        emo_of_agent = r[0]
        reason_of_agent = dagent_llm.history[-1].reason

        # Similar blocks for evaluating user emotions, problem resolution, and process compliance...
        
        return {
            "客服情绪评估": emo_of_agent,
            "客服情绪原因": reason_of_agent,
            "用户情绪评估": emo_of_user,
            "用户情绪原因": reason_of_user,
            "用户问题解决评估": is_user_problem_solved,
            "用户问题解决原因": reason_of_user_problem_solved,
            "解答流程规范评估": is_answer_process_standard,
            "解答流程规范原因": reason_of_answer_process_standard
        }
    except Exception as e:
        console.print(f"[red]Error evaluating content: {str(e)}[/red]")
        return {}
```

#### 4. **Main Script**
The main script processes a directory of WAV files, converts them to the required format, runs speaker diarization, performs transcriptions, and evaluates the conversations based on the criteria listed.

```python
if __name__ == "__main__":
    input_dir = "/datasets_hdd/customer_downloadwavs/20241014/"
    output_dir = "outputs/"
    txt_dir = os.path.join(output_dir, "txt")
    os.makedirs(txt_dir, exist_ok=True)

    csv_file = os.path.join(output_dir, "output.csv")
    with open(csv_file, mode="w", newline="", encoding="utf-8") as file:
        writer = csv.writer(file)
        writer.writerow(["ID", "客服情绪评估", "客服情绪原因", "用户情绪评估", "用户情绪原因", 
                         "用户问题解决评估", "用户问题解决原因", "解答流程规范评估", "解答流程规范原因"])

        for filename in os.listdir(input_dir):
            if filename.endswith(".wav") and "channel" not in filename:
                try:
                    file_path = os.path.join(input_dir, filename)
                    file_id = os.path.splitext(filename)[0]
                    
                    # Audio preprocessing using ffmpeg
                    file_path_new = os.path.join(output_dir, 'tmp_wav', f"{file_id}.wav")
                    subprocess.run(f"ffmpeg -y -i  {file_path} -ac 1 -ar 16000 {file_path_new}", shell=True)
                    
                    # Diarization and transcription
                    content = get_diarization_content(file_path_new)
                    
                    # Evaluate the conversation
                    results = evaluate_wav_file(content)
                    if results:
                        writer.writerow([file_id] + list(results.values()))

                except Exception as e:
                    console.print(f"[red]Error processing file {filename}: {str(e)}[/red]")

    console.print(f"[bold green]Process completed! Results are saved in {output_dir}[/bold green]")
```

### Conclusion
This demo illustrates how to integrate speaker diarization, speech transcription, emotion analysis, and dialogue evaluation into a single system for inspecting the quality of customer service interactions. The combination of `dagent_llm`, `dguard`, and `dspeech` ensures comprehensive analysis of both speech content and emotions, providing valuable insights for customer service improvement.


## Contributing
Contributions to the LLM Package are welcome! Please fork the repository, make your changes, and submit a pull request.
## License
This project is licensed under the MIT License - see the LICENSE file for details.
## Contact
For any questions or suggestions, please email Zhao Sheng at zhaosheng@nuaa.edu.cn.

            

Raw data

            {
    "_id": null,
    "home_page": "https://gitee.com/iint/dagent_llm",
    "name": "dagent-llm",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": null,
    "keywords": null,
    "author": "Zhao Sheng",
    "author_email": "zhaosheng@nuaa.edu.cn",
    "download_url": "https://files.pythonhosted.org/packages/8a/fc/3cfc0552667985bbf0ecc6602db744acca43672f5ab1a9f1715144e39214/dagent_llm-0.2.0.tar.gz",
    "platform": null,
    "description": "<p align=\"center\">\n  <img src=\"dagent_llm.png\" alt=\"DCheck Logo\" width=\"250\">\n</p>\n\n# DAgent: Command Line Interface for Language Model Operations\n[\u4e2d\u6587](README_zh.md) | English\n## Overview\nThe LLM Package is a Python-based command-line interface (CLI) that provides an easy way to interact with Large Language Models (LLMs). It allows users to chat with the model, make choices based on given options, and more. This package is designed to be simple, intuitive, and extendable for various LLM operations.\n## Features\n- Chat with the LLM and receive responses.\n- Present options to the LLM and get a choice.\n- Choose an option and provide arguments for further processing.\n- Few-shot learning capabilities for better context understanding.\n- Logging of conversation history for future reference.\n## Installation\nTo install the LLM Package, run the following command:\n```bash\npip install dagent_llm\n```\nEnsure that you have Python 3.6 or later installed on your system.\n## Help\nTo view the available commands and options, use the `help` flag:\n```bash\ndagent_llm help\n```\nThis will display the list of available commands and their descriptions.\n> Note : Dagent_llm assumes that you have configured the environment variables through [dsqlenv](https://pypi.org/project/dsqlenv/), and the system will read the necessary information from [dsqlenv](https://pypi.org/project/dsqlenv/).\n```\nD-Agent LLM Command Line Interface\n\nUsage: dagent_llm \n\nAvailable Commands:\n  chat             Send a message to the LLM and get a response.\n  choose           Present options to the LLM and get a choice.\n  choose_with_args Choose an option and provide arguments.\n\nOptions for 'chat' command:\n  --message         The message to send to the LLM.\n  --llm_server      Specify the LLM server to use.\n  --role            Specify the role of the message sender (default: 'human').\n\nOptions for 'choose' command:\n  --options         List of options to choose from.\n  --prompt          The prompt for choosing.\n  --need-reason     Ask the LLM to provide reasons for the choice.\n  --multiple        Allow the LLM to select multiple options.\n  --notes           Additional notes to add to the prompt.\n  --examples        Few-shot learning examples to guide the choice.\n\nOptions for 'choose_with_args' command:\n  --options         List of options to choose from.\n  --prompt          The prompt for choosing.\n  --option-type     The type of options being chosen.\n  --need-reason     Provide reasons for the choice.\n  --multiple        Allow multiple selections.\n  --notes           Additional notes to add to the prompt.\n  --examples        Few-shot learning examples to guide the choice.\n\nVersion: 0.1.0 | 2024-10-18\nCopyright: \u00a9 2024 VoiceCodeAI, Singapore\n```\n\n## Dependencies\n- Python 3.6+\n- dsqlenv\n- langchain_core\n- langchain_openai\n\n## Usage\n### Chatting with the LLM\nTo send a message to the LLM and receive a response, use the `chat` command:\n```bash\ndagent_llm chat --message \"Hello, how are you?\" --role human\n```\nThe `--role` flag can be set to `human`, `ai`, or `system` depending on the context of the message.\n### Making a Choice\nTo present options to the LLM and get a choice, use the `choose` command:\n```bash\ndagent_llm choose --options \"Option 1\" \"Option 2\" \"Option 3\" --prompt \"Choose an option\" --need-reason --multiple\n```\nThe `--need-reason` flag will ask the LLM to provide reasons for the choice, and the `--multiple` flag allows the selection of multiple options.\n### Choosing with Arguments\nTo choose an option and provide arguments, use the `choose_with_args` command:\n```bash\ndagent_llm choose_with_args --options \"Option 1\" \"Option 2\" \"Option 3\" --prompt \"Choose an option and provide arguments\" --option-type \"type\" --need-reason --multiple\n```\nThe `--option-type` flag describes the type of options being chosen.\n### Providing Few-Shot Examples\nYou can provide few-shot examples to guide the LLM using the `examples` argument:\n```bash\ndagent_llm choose --options ... --prompt ... --examples \"Example 1\" \"Example 2\"\n```\n### Adding Notes\nAdditional notes can be added to the prompt using the `notes` argument:\n```bash\ndagent_llm choose --options ... --prompt ... --notes \"Note 1\" \"Note 2\"\n```\n## Demo\nHere's a simple demo to demonstrate chatting with the LLM:\n```bash\n# Chat with the LLM\ndagent_llm chat --message \"What's the weather like today?\" --role human\n# Output:\n# LLM response: The weather is sunny with a few clouds.\n```\n\n## Python API\nThe LLM Package can also be used as a Python library. Here's an example of how to chat with the LLM using the Python API:\n```python\nfrom dagent_llm import LLM\nfrom langchain_core.messages import HumanMessage, SystemMessage, AIMessage\n\n# Initialize the LLM model with a specific engine\nmodel = LLM(\"deepseek\")  # 'deepseek' is the engine being used for LLM\n# Note: Directly starting with llm_server requires you to have installed dsqlenv and configured it.\n# For example: {llm_server}_api_key, {llm_server}_base_url, {llm_server}_model, etc.\n# Please refer to [dsqlenv](https://pypi.org/project/dsqlenv/) for specific configuration methods.\n\n# Or you can specify the complete engine information\nmodel = LLM(\n    llm_server=\"deepseek\",\n    max_retries=3, # Max retry times\n    ###################################\n    # OpenAI API parameters\n    temperature=0.7,\n    api_key=\"your_api_key\",\n    base_url=\"https://api.deepseek.com\",\n    model_name=\"deepseek-chat\", \n    ###################################\n    history=[] # Same as langchain_core.messages\n)\n\n\n# Simple interaction with the model\nr = model.chat(\"Tell me about yourself\")\nprint(r.content)  # Outputs the response from the model\n\n# Define a list of functions with their names and inputs\nfunctions_info = [\n    {\"name\": \"get_data\", \"input\": [\"id\"], \"example_input\": \"a\"},\n    {\"name\": \"insert_data\", \"input\": [\"id\", \"data\"], \"example_input\": [\"a\", \"b\"]},\n    {\"name\": \"update_data\", \"input\": [\"id\", \"data\"], \"example_input\": [\"a\", \"b\"]},\n    {\"name\": \"delete_data\", \"input\": [\"id\"], \"example_input\": \"a\"},\n]\n\n# Example 1: Selecting a function based on user input, including reasons for choice\n# Here, the model will be asked to select a function and provide the necessary arguments.\nr = model.function_choose(\n    functions_info,                      # List of functions to choose from\n    \"Add a record with key-value pair abc and 123\",  # The prompt asking what to do\n    need_reason=True,                    # Model must provide a reason for its choice\n    multiple=False,                      # Single function selection allowed\n    add_to_history=True                  # Add this interaction to the conversation history\n)\nprint(r)  # Outputs the selected function and arguments\n\n\n# Example 2: Function selection with additional context such as examples and notes\n# This provides the model with extra guidance on how to make its decision\nr2 = model.function_choose(\n    functions_info,\n    \"Delete record with key abc\",        # Instruction for deletion operation\n    need_reason=True,                    # Model must provide reasoning\n    multiple=False,                      # Only one function can be selected\n    add_to_history=True,                 # Record this interaction\n    examples=[                           # Example to guide the model\n        \"Add a record with key-value pair abc and 123 -> insert_data('abc', '123')\"\n    ],\n    notes=[                              # Important notes for the operation\n        \"Delete operation is irreversible\",  \n        \"This will delete all records with key 'abc'\"\n    ]\n)\nprint(r2)  # Outputs the selected function and explanation\n\n\n# Example 3: Simple selection scenario for choosing from a list of food options\n# Multiple selections are allowed in this case, and the model needs to justify its choice\nfoods = [\"Snail noodles\", \"Rice noodles\", \"Beef noodles\", \"Egg noodles\", \"Vegetable salad\", \"Boiled beef\"]\nr = model.choose(\n    foods,                               # List of options to choose from\n    \"What can I eat while on a diet?\",   # The question or prompt\n    \"Food name\",                         # Type of options being chosen\n    need_reason=True,                    # Model must provide reasons for its choices\n    multiple=True,                       # Multiple choices allowed (diet-friendly foods)\n    add_to_history=True                  # Record the conversation\n)\nprint(r)  # Outputs the selected food(s) and reason(s)\n\n# Review conversation history to see how previous interactions were logged\nprint(model.history)\n\n# If token information is needed (optional debugging for developers):\n# print(model.input_tokens)\n```\n## APP: Telephone Customer Service Quality Inspection System: Combining `dagent_llm`, `dguard`, and `dspeech`\n\nThis demo showcases how to build a quality inspection system for telephone customer service using the following components:\n- **`dagent_llm`**: A large language model (LLM) used for evaluating the dialogue content, identifying emotions, solving user problems, and ensuring compliance with customer service standards.\n- **`dguard`**: A diarization model used to identify speakers and segment audio files by speaker turns.\n- **`dspeech`**: A speech-to-text (STT) model used for transcribing audio content and classifying emotions.\n\n### Demo Features\nThis system processes recorded customer service calls, providing:\n1. **Speaker diarization**: Identifies different speakers from the audio.\n2. **Emotion analysis**: Assesses emotions for both customer service agents and customers.\n3. **Service quality evaluation**: Determines whether customer problems are solved and evaluates if the agent followed proper procedures.\n\n### Requirements\n- Python 3.8+\n- `dagent_llm`, `dguard`, and `dspeech` installed\n- Additional Python libraries: `rich`, `os`, `csv`, `subprocess`\n\n### System Workflow\n1. **Input WAV Files**: The system takes in audio files (WAV format) from the customer service call recordings.\n2. **Audio Preprocessing**: The audio is downsampled to a single channel (16 kHz) using `ffmpeg`.\n3. **Speaker Diarization**: The `dguard` model identifies different speakers in the audio and segments it based on speaker turns.\n4. **Speech Transcription**: The `dspeech` model transcribes each speaker\u2019s segment into text.\n5. **Emotion Classification**: For segments longer than a set threshold (e.g., 2 seconds), the system classifies emotions using the `dspeech` model.\n6. **Dialogue Evaluation**: The system uses `dagent_llm` to assess:\n   - Agent's emotions: Whether they exhibit negative emotions.\n   - Customer's emotions: Whether they are satisfied or dissatisfied with the service.\n   - Problem resolution: Whether the customer\u2019s issue was solved.\n   - Procedural compliance: Whether the agent followed proper service procedures.\n7. **Results**: The system outputs a CSV file summarizing the evaluation results, along with individual text files for each conversation.\n\n### Code Overview\n\nBelow is a breakdown of the main functions and their roles:\n\n#### 1. **rich_print**\nThis function uses the `rich` library to color-code and format output. It highlights speaker turns and emotions.\n\n```python\ndef rich_print(content):\n    colors = [\"red\", \"green\", \"blue\", \"yellow\", \"magenta\", \"cyan\"]\n    for idx, line in enumerate(content.split(\"\\n\")):\n        if \":\" not in line:\n            continue\n        spk_id = line.split(\":\")[0].split(\" \")[-1]\n        console.print(f\"[{colors[int(spk_id) % 6]}]{line}\")\n```\n\n#### 2. **get_diarization_content**\nThis function processes a WAV file to generate speaker-diarized transcriptions. For each speaker, it transcribes the speech and classifies emotions if the speaking duration exceeds the `emotion_time_threshold`.\n\n```python\ndef get_diarization_content(file_path, emotion_time_threshold=2):\n    try:\n        r = dm_model.diarize(file_path)\n        all_content = \"\"\n        last_spk = \"\"\n        for data in r:\n            spk_label = data[3]\n            start_time = data[1]\n            end_time = data[2]\n            generate_text = stt_model.transcribe_file(file_path, start=start_time, end=end_time)\n            if end_time - start_time > emotion_time_threshold:\n                emotion = stt_model.emo_classify_file(file_path, start=start_time, end=end_time)\n                emotion_label = emotion[\"labels\"][emotion[\"scores\"].index(max(emotion[\"scores\"]))]\n                emotion_score = max(emotion[\"scores\"])\n                emotion_text = f\"(emotion\uff1a{emotion_label} with score: {emotion_score:.2f})\"\n            else:\n                emotion_text = \"\"\n            if spk_label != last_spk:\n                all_content += f\"\\nSpeaker {spk_label}: {generate_text} \" + emotion_text\n                last_spk = spk_label\n            else:\n                all_content += f\" {generate_text}\"\n        return all_content\n    except Exception as e:\n        console.print(f\"[red]Error processing file {file_path}: {str(e)}[/red]\")\n        return \"\"\n```\n\n#### 3. **evaluate_wav_file**\nThis function evaluates the quality of the customer service conversation using the `dagent_llm`. It provides:\n- Agent emotion evaluation\n- Customer satisfaction\n- Problem resolution assessment\n- Procedural compliance evaluation\n\n```python\ndef evaluate_wav_file(content):\n    try:\n        chooses = [\"\u7b26\u5408\u8981\u6c42\uff08\u65e0\u8d1f\u9762\u60c5\u7eea\uff09\", \"\u4e0d\u7b26\u5408\u8981\u6c42\uff08\u6709\u8d1f\u9762\u60c5\u7eea\uff09\"]\n        prompt = f\"<\u5bf9\u8bdd\u5185\u5bb9>\\n{content}<\u5bf9\u8bdd\u5185\u5bb9>\\n\u8bf7\u4f60\u6839\u636e\u5bf9\u8bdd\u5185\u5bb9\u8bc4\u4f30\u5ba2\u670d\u4eba\u5458\u7684\u60c5\u7eea\u662f\u5426\u7b26\u5408\u8981\u6c42...\"\n        r = dagent_llm.choose(chooses, prompt, \"\u60c5\u7eea\u662f\u5426\u7b26\u5408\u8981\u6c42\", need_reason=True)\n        emo_of_agent = r[0]\n        reason_of_agent = dagent_llm.history[-1].reason\n\n        # Similar blocks for evaluating user emotions, problem resolution, and process compliance...\n        \n        return {\n            \"\u5ba2\u670d\u60c5\u7eea\u8bc4\u4f30\": emo_of_agent,\n            \"\u5ba2\u670d\u60c5\u7eea\u539f\u56e0\": reason_of_agent,\n            \"\u7528\u6237\u60c5\u7eea\u8bc4\u4f30\": emo_of_user,\n            \"\u7528\u6237\u60c5\u7eea\u539f\u56e0\": reason_of_user,\n            \"\u7528\u6237\u95ee\u9898\u89e3\u51b3\u8bc4\u4f30\": is_user_problem_solved,\n            \"\u7528\u6237\u95ee\u9898\u89e3\u51b3\u539f\u56e0\": reason_of_user_problem_solved,\n            \"\u89e3\u7b54\u6d41\u7a0b\u89c4\u8303\u8bc4\u4f30\": is_answer_process_standard,\n            \"\u89e3\u7b54\u6d41\u7a0b\u89c4\u8303\u539f\u56e0\": reason_of_answer_process_standard\n        }\n    except Exception as e:\n        console.print(f\"[red]Error evaluating content: {str(e)}[/red]\")\n        return {}\n```\n\n#### 4. **Main Script**\nThe main script processes a directory of WAV files, converts them to the required format, runs speaker diarization, performs transcriptions, and evaluates the conversations based on the criteria listed.\n\n```python\nif __name__ == \"__main__\":\n    input_dir = \"/datasets_hdd/customer_downloadwavs/20241014/\"\n    output_dir = \"outputs/\"\n    txt_dir = os.path.join(output_dir, \"txt\")\n    os.makedirs(txt_dir, exist_ok=True)\n\n    csv_file = os.path.join(output_dir, \"output.csv\")\n    with open(csv_file, mode=\"w\", newline=\"\", encoding=\"utf-8\") as file:\n        writer = csv.writer(file)\n        writer.writerow([\"ID\", \"\u5ba2\u670d\u60c5\u7eea\u8bc4\u4f30\", \"\u5ba2\u670d\u60c5\u7eea\u539f\u56e0\", \"\u7528\u6237\u60c5\u7eea\u8bc4\u4f30\", \"\u7528\u6237\u60c5\u7eea\u539f\u56e0\", \n                         \"\u7528\u6237\u95ee\u9898\u89e3\u51b3\u8bc4\u4f30\", \"\u7528\u6237\u95ee\u9898\u89e3\u51b3\u539f\u56e0\", \"\u89e3\u7b54\u6d41\u7a0b\u89c4\u8303\u8bc4\u4f30\", \"\u89e3\u7b54\u6d41\u7a0b\u89c4\u8303\u539f\u56e0\"])\n\n        for filename in os.listdir(input_dir):\n            if filename.endswith(\".wav\") and \"channel\" not in filename:\n                try:\n                    file_path = os.path.join(input_dir, filename)\n                    file_id = os.path.splitext(filename)[0]\n                    \n                    # Audio preprocessing using ffmpeg\n                    file_path_new = os.path.join(output_dir, 'tmp_wav', f\"{file_id}.wav\")\n                    subprocess.run(f\"ffmpeg -y -i  {file_path} -ac 1 -ar 16000 {file_path_new}\", shell=True)\n                    \n                    # Diarization and transcription\n                    content = get_diarization_content(file_path_new)\n                    \n                    # Evaluate the conversation\n                    results = evaluate_wav_file(content)\n                    if results:\n                        writer.writerow([file_id] + list(results.values()))\n\n                except Exception as e:\n                    console.print(f\"[red]Error processing file {filename}: {str(e)}[/red]\")\n\n    console.print(f\"[bold green]Process completed! Results are saved in {output_dir}[/bold green]\")\n```\n\n### Conclusion\nThis demo illustrates how to integrate speaker diarization, speech transcription, emotion analysis, and dialogue evaluation into a single system for inspecting the quality of customer service interactions. The combination of `dagent_llm`, `dguard`, and `dspeech` ensures comprehensive analysis of both speech content and emotions, providing valuable insights for customer service improvement.\n\n\n## Contributing\nContributions to the LLM Package are welcome! Please fork the repository, make your changes, and submit a pull request.\n## License\nThis project is licensed under the MIT License - see the LICENSE file for details.\n## Contact\nFor any questions or suggestions, please email Zhao Sheng at zhaosheng@nuaa.edu.cn.\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "A package for LLM operations.",
    "version": "0.2.0",
    "project_urls": {
        "Homepage": "https://gitee.com/iint/dagent_llm"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "1511be866ae29481c000ca09321daf40970e36ff3634b92ca6735d0b609f1020",
                "md5": "ce2ae6cf20bb33de4d43ef6157877d3c",
                "sha256": "cbc63690fbb59035e80ddaf8d2b92162e80a3bc0fdc8ea9102d0ec9f9c57cf10"
            },
            "downloads": -1,
            "filename": "dagent_llm-0.2.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "ce2ae6cf20bb33de4d43ef6157877d3c",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 16946,
            "upload_time": "2024-10-20T03:03:04",
            "upload_time_iso_8601": "2024-10-20T03:03:04.265891Z",
            "url": "https://files.pythonhosted.org/packages/15/11/be866ae29481c000ca09321daf40970e36ff3634b92ca6735d0b609f1020/dagent_llm-0.2.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8afc3cfc0552667985bbf0ecc6602db744acca43672f5ab1a9f1715144e39214",
                "md5": "6245736ed93cc367f2e416fc88f8bcf1",
                "sha256": "1028f020587f4c68e5f40cdce0a0775e6050ec5e9c4526bf04fd4db0a2ca6860"
            },
            "downloads": -1,
            "filename": "dagent_llm-0.2.0.tar.gz",
            "has_sig": false,
            "md5_digest": "6245736ed93cc367f2e416fc88f8bcf1",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 23215,
            "upload_time": "2024-10-20T03:03:06",
            "upload_time_iso_8601": "2024-10-20T03:03:06.409859Z",
            "url": "https://files.pythonhosted.org/packages/8a/fc/3cfc0552667985bbf0ecc6602db744acca43672f5ab1a9f1715144e39214/dagent_llm-0.2.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-20 03:03:06",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "dagent-llm"
}
        
Elapsed time: 0.85191s