Msglm banner
AnswerDotAI AnswerDotAI

Msglm

Development community

Description

msglm makes it a little easier to create messages for language models like Claude and OpenAI GPTs.

Installation

This entry records only its repository, not the path inside it, so there is no exact command to give. Open the source below and copy the folder into ~/.claude/skills/, or the file into ~/.claude/agents/.

README

msglm

Installation

Install the latest version from pypi

$ pip install msglm

Usage

To use an LLM we need to structure our messages in a particular format.

Here’s an example of a text chat from the OpenAI docs.

from openai import OpenAI
client = OpenAI()

completion = client.chat.completions.create(
  model="gpt-4o",
  messages=[
    {"role": "user", "content": "What's the Wild Atlantic Way?"}
  ]
)

Generating the correct format for a particular API can get tedious. The goal of *msglm* is to make it easier.

The examples below will show you how to use *msglm* for text and image chats with OpenAI and Anthropic.

Text Chats

For a text chat simply pass a list of strings and the api format (e.g. “openai”) to **mk_msgs** and it will generate the correct format.

mk_msgs(["Hello, world!", "some assistant response"], api="openai")
[
    {"role": "user", "content": "Hello, world!"},
    {"role": "assistant", "content": "Some assistant response"}
]

anthropic

from msglm import mk_msgs_anthropic as mk_msgs
from anthropic import Anthropic
client = Anthropic()

r = client.messages.create(
    model="claude-3-haiku-20240307",
    max_tokens=1024,
    messages=[mk_msgs(["Hello, world!", "some LLM response"])]
)
print(r.content[0].text)

openai

from msglm import mk_msgs_openai as mk_msgs
from openai import OpenAI

client = OpenAI()
r = client.chat.completions.create(
  model="gpt-4o-mini",
  messages=[mk_msgs(["Hello, world!", "some LLM response"])]
)
print(r.choices[0].message.content)

Image Chats

For an image chat simply pass the raw image bytes in a list with your question to *mk_msgs* and it will generate the correct format.

mk_msg([img, "What's in this image?"], api="anthropic")
[
    {
        "role": "user", 
        "content": [
            {"type": "image", "source": {