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.1.tar.gz (17.0 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.1-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for mtkresearch-0.3.1.tar.gz
Algorithm Hash digest
SHA256 b0aec84d3f4610015fd26982309d7ee7317fe66fae573ddea95de3e46eaceb9c
MD5 d65c32d431b24e7bf5cc8a5b16ba3749
BLAKE2b-256 dfa60c1d3b49b2a62b2f82b7e91488d3909317f9bf613a98b4d1c8c54992ffc2

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for mtkresearch-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 90ce65930921befc73dc5f8d57f828b7ff4a8ea1bf7c25dfc45db95e114c94a7
MD5 4425987846bfd15796a9bd22026d65bc
BLAKE2b-256 d69c80690f341acce649f293fbcf064a568d6955c0df836eb298e2aed78baa5c

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