Skip to main content

mtkresearch

Project description

MTK Research Package

Setup

$ pip install mtkresearch

MRPromptV3

MRPromptV3 is a class designed to facilitate the creation and management of prompts for conversational AI systems. This class provides methods to generate prompts based on a series of conversations, including support for function calls and handling various types of user inputs such as text and image.

Initialization

To initialize an instance of MRPromptV3, simply import the class and create an instance:

from mtkresearch.llm.prompt import MRPromptV3
prompt = MRPromptV3()

Methods

get_prompt

Generates a prompt based on the provided conversations and optional functions.

Parameters:
* `conversations` (list): A list of conversation dictionaries, each containing role and content.
* `functions` (list, optional): A list of function definitions that the assistant can use. Default is `None`.
* `add_bos_token` (bool, optional): Whether to add a beginning-of-sequence token. Default is `False`.
* `training` (bool, optional): Whether the prompt is for training purposes. Default is `False`.

Returns:
* `str`: The generated prompt.

Example:

python
conversations = [
    {"role": "user", "content": "What is the weather of Boston?"}
]
functions = [
    {
        'name': 'get_current_weather',
        'description': 'Get the current weather',
        'parameters': {
            'type': 'object',
            'properties': {
                'location': {'type': 'string', 'description': 'The city and state, e.g. San Francisco, CA'},
                'unit': {'type': 'string', 'enum': ['celsius', 'fahrenheit']}
            },
            'required': ['location']
        }
    }
]

result = prompt.get_prompt(conversations, functions)
print(result)

parse_generated_str

Parses a generated string to extract the role and content, including function calls.

Parameters:
* `generated_str` (str): The generated string to parse.

Returns:
* `dict`: A dictionary containing the parsed role and content.

Example:

generated_str = "<|use_tool|>[get_current_weather(location='Boston, MA')]<|eot_id|>"
parsed_result = prompt.parse_generated_str(generated_str)
print(parsed_result)
# Expected: 
# {
#     'role': 'assistant',
#     'tool_calls': [
#         {
#             'type': 'function',
#             'function': {
#                 'name': 'get_current_weather',
#                 'arguments': '{"location": "Boston, MA"}'
#             }
#         }
#     ]
# }

Structure of conversations

Basic Structure

Each message in the conversations list has the following basic structure:

{
    "role": string ("system" | "user" | "assistant" | "tool"),
    "content": string | list,
    ...
}
  • role: Specifies the role of the speaker. It can be one of the following:
    • "system": Represents system messages, usually providing context or instructions.
    • "user": Represents messages from the user.
    • "assistant": Represents messages from the assistant.
    • "tool": Represents responses from tools or functions called by the assistant.
  • content: The content of the message. It can be a string or a list of dictionaries, depending on the type of message.

System Message

A system message provides context or instructions for the conversation.

{
    "role": "system",
    "content": "YOUR SYSTEM MESSAGE"
}

User Message

A user message contains the user's input.

{
    "role": "user",
    "content": "YOUR PROBLEM"
}

User Message with Image

A user message containing text, an image, and bounding box information.

{
    "role": "user",
    "content": [
        {
            "type": "image",
            "image_path": "/path/to/image.png",
        },
        {
            "type": "text",
            "text": "In the above image, how many dogs in "
        },
        {
            "type": "bbox",
            "width": 1024,
            "height": 768,
            "coords": [[0, 0, 512, 384]]
        },
        {
            "type": "text",
            "text": " ?"
        }
    ]
}
  • type: Specifies the type of content. It can be "text", "image", or "bbox".
  • text: The text content (for "text" types).
  • image_path: The path to the image file (for "image" type).
  • width and height: The dimensions of the image.
  • coords: The coordinates of the bounding box (for "bbox" type).

Assistant Message

An assistant message contains the assistant's response.

{
    "role": "assistant",
    "content": "RESPONSE"
}

Tool Call

A tool call message contains information about a function call made by the assistant.

{
    "role": "assistant",
    "tool_calls": [
        {
            'id': 'call_8jLWqlXaY3OisD24IHJLwD3G',
            'type': 'function',
            'function': {
                'arguments': "{\"location\": \"Boston, MA\"}",
                'name': 'get_current_weather'
            }
        }
    ]
}

Tool Response

A tool response message contains the response from a tool or function.

{
    "role": "tool",
    "tool_call_id": "call_8jLWqlXaY3OisD24IHJLwD3G",
    "name": "get_current_weather",
    "content": "{\"temperature\": \"22 celsius\"}"
}

Structure of functions

Basic Structure

Each function description in the functions list has the following basic structure:

{
    "name": str,
    "description": str,
    "parameters": dict,
    "required": list
}
  • name: The name of the function, which should be short, clear, and descriptive of its core functionality. The format must be a string without spaces or special characters. For examples, "get_weather", "calculate_shipping", "translate_text".
  • description: A concise text explaining the purpose and function of the API. Use natural language. Be brief but complete, avoiding unnecessary technical jargon or ambiguity.
  • parameters: Defines the parameters and their structure required by the function, following the JSON Schema standard. Format as following:
    • type: Must be "object".
    • properties: Defines the specific attributes of each parameter, including name, type, and description. Each parameter definition includes:
      • type: The data type of the parameter ("string", "number", "boolean", "array", "object").
      • description: Explains the purpose of the parameter.
      • Optional attributes such as "enum", "default", etc.
  • required: Lists the names of all required parameters (optional).

For example:

{
  "name": "get_weather",
  "description": "Fetches the current weather for a specific location.",
  "parameters": {
    "type": "object",
    "properties": {
      "location": {
        "type": "string",
        "description": "The name of the city or geographic coordinates (latitude,longitude)."
      },
      "unit": {
        "type": "string",
        "description": "The unit of temperature measurement. Options are 'metric' (Celsius), 'imperial' (Fahrenheit), or 'standard' (Kelvin).",
        "enum": ["metric", "imperial", "standard"],
        "default": "metric"
      },
      "language": {
        "type": "string",
        "description": "The language code for the weather description (e.g., 'en' for English, 'fr' for French).",
        "default": "en"
      }
    },
    "required": ["location"]
  }
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mtkresearch-0.3.2.tar.gz (17.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mtkresearch-0.3.2-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file mtkresearch-0.3.2.tar.gz.

File metadata

  • Download URL: mtkresearch-0.3.2.tar.gz
  • Upload date:
  • Size: 17.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.23

File hashes

Hashes for mtkresearch-0.3.2.tar.gz
Algorithm Hash digest
SHA256 447256da3f1f5e87bde323b4f6154b1baaa474ce5672d005ddb615e800bb855f
MD5 24d156592fa775445b50e07076b378bd
BLAKE2b-256 0a7e65ba1b70df76c72b52da6a29668656f332b315b7e3316bf7bdd6c3887ba8

See more details on using hashes here.

File details

Details for the file mtkresearch-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: mtkresearch-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 12.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.23

File hashes

Hashes for mtkresearch-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5c10759d68f4cc8881c8eab03112b1bd3051bd56ff3d90a16e0cfa8fe22fdfcc
MD5 1c0b1418df5ff311b7e3570a004994ab
BLAKE2b-256 decfe615430e94ff6459ae2ac4dce5a789f03550a320abd9646dad43e0417d24

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page