vm-x-ai-langchain


Namevm-x-ai-langchain JSON
Version 0.2.0 PyPI version JSON
download
home_pagehttps://github.com/vm-x-ai/vm-x-ai-sdk
SummaryVM-X AI Langchain Python SDK
upload_time2024-08-23 16:32:32
maintainerVM-X Engineering
docs_urlNone
authorVM-X Engineering
requires_python<4,>=3.8.1
licenseMIT
keywords vm-x ai sdk python langchain
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # VM-X SDK for Python Langchain

## Description

VM-X AI SDK client for Python Langchain

## Installation

```bash
pip install vm-x-ai-langchain
```

```bash
poetry add vm-x-ai-langchain
```

## Usage

### Non-Streaming

```python
from vmxai_langchain import ChatVMX

llm = ChatVMX(
    resource="default",
)

messages = [
    (
        "system",
        "You are a helpful translator. Translate the user sentence to French.",
    ),
    ("human", "I love programming."),
]
result = llm.invoke(messages)
```

### Streaming

```python
from vmxai_langchain import ChatVMX

llm = ChatVMX(
    resource="default",
)

messages = [
    (
        "system",
        "You are a helpful translator. Translate the user sentence to French.",
    ),
    ("human", "I love programming."),
]

for chunk in llm.stream(messages):
    print(chunk.content, end="", flush=True)
```

### Function Calling

#### Decorator

```python
from langchain_core.messages import HumanMessage, ToolMessage
from langchain_core.tools import tool
from vmxai_langchain import ChatVMX


@tool
def add(a: int, b: int) -> int:
    """Adds a and b.

    Args:
        a: first int
        b: second int
    """
    return a + b


@tool
def multiply(a: int, b: int) -> int:
    """Multiplies a and b.

    Args:
        a: first int
        b: second int
    """
    return a * b


tools = [add, multiply]
llm = ChatVMX(
    resource="default",
)

llm_with_tools = llm.bind_tools(tools)
query = "What is 3 * 12? Also, what is 11 + 49?"

messages = [HumanMessage(query)]
ai_msg = llm_with_tools.invoke(messages)
messages.append(ai_msg)

for tool_call in ai_msg.tool_calls:
    selected_tool = {"add": add, "multiply": multiply}[tool_call["name"].lower()]
    tool_output = selected_tool.invoke(tool_call["args"])
    messages.append(ToolMessage(tool_output, tool_call_id=tool_call["id"]))

print(llm_with_tools.invoke(messages))
```

#### Pydantic

```python
from langchain_core.pydantic_v1 import BaseModel, Field
from vmxai_langchain import ChatVMX
from vmxai_langchain.output_parsers.tools import PydanticToolsParser


# Note that the docstrings here are crucial, as they will be passed along
# to the model along with the class name.
class add(BaseModel):
    """Add two integers together."""

    a: int = Field(..., description="First integer")
    b: int = Field(..., description="Second integer")


class multiply(BaseModel):
    """Multiply two integers together."""

    a: int = Field(..., description="First integer")
    b: int = Field(..., description="Second integer")


tools = [add, multiply]

llm = ChatVMX(
    resource="default",
)

llm_with_tools = llm.bind_tools(tools) | PydanticToolsParser(tools=[multiply, add])

query = "What is 3 * 12? Also, what is 11 + 49?"

print(llm_with_tools.invoke(query))

```

#### Function Calling Streaming

```python
from langchain_core.pydantic_v1 import BaseModel, Field
from vmxai_langchain import ChatVMX
from vmxai_langchain.output_parsers.tools import PydanticToolsParser


# Note that the docstrings here are crucial, as they will be passed along
# to the model along with the class name.
class add(BaseModel):
    """Add two integers together."""

    a: int = Field(..., description="First integer")
    b: int = Field(..., description="Second integer")


class multiply(BaseModel):
    """Multiply two integers together."""

    a: int = Field(..., description="First integer")
    b: int = Field(..., description="Second integer")


tools = [add, multiply]

llm = ChatVMX(
    resource="default",
)

llm_with_tools = llm.bind_tools(tools) | PydanticToolsParser(tools=[multiply, add])

query = "What is 3 * 12? Also, what is 11 + 49?"

for chunk in llm_with_tools.stream(query):
    print(chunk)
```

### Structured Output

```python
from langchain_core.pydantic_v1 import BaseModel, Field
from vmxai_langchain import ChatVMX


class Joke(BaseModel):
    setup: str = Field(description="The setup of the joke")
    punchline: str = Field(description="The punchline to the joke")


llm = ChatVMX(resource="default")
structured_llm = llm.with_structured_output(Joke, strict=True)

print(structured_llm.invoke("Tell me a joke about cats"))

```

## Limitations

1. Async client is not supported.
2. `json_mode` and `json_schema` Structured output are not supported.

## [Change Log](./CHANGELOG.md)

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/vm-x-ai/vm-x-ai-sdk",
    "name": "vm-x-ai-langchain",
    "maintainer": "VM-X Engineering",
    "docs_url": null,
    "requires_python": "<4,>=3.8.1",
    "maintainer_email": "eng@vm-x.ai",
    "keywords": "VM-X, AI, SDK, Python, LangChain",
    "author": "VM-X Engineering",
    "author_email": "eng@vm-x.ai",
    "download_url": "https://files.pythonhosted.org/packages/80/cc/abc76fbc1a2de83f4c2c44dc2f457dac6adb077caf2dd69ad6722761b888/vm_x_ai_langchain-0.2.0.tar.gz",
    "platform": null,
    "description": "# VM-X SDK for Python Langchain\n\n## Description\n\nVM-X AI SDK client for Python Langchain\n\n## Installation\n\n```bash\npip install vm-x-ai-langchain\n```\n\n```bash\npoetry add vm-x-ai-langchain\n```\n\n## Usage\n\n### Non-Streaming\n\n```python\nfrom vmxai_langchain import ChatVMX\n\nllm = ChatVMX(\n    resource=\"default\",\n)\n\nmessages = [\n    (\n        \"system\",\n        \"You are a helpful translator. Translate the user sentence to French.\",\n    ),\n    (\"human\", \"I love programming.\"),\n]\nresult = llm.invoke(messages)\n```\n\n### Streaming\n\n```python\nfrom vmxai_langchain import ChatVMX\n\nllm = ChatVMX(\n    resource=\"default\",\n)\n\nmessages = [\n    (\n        \"system\",\n        \"You are a helpful translator. Translate the user sentence to French.\",\n    ),\n    (\"human\", \"I love programming.\"),\n]\n\nfor chunk in llm.stream(messages):\n    print(chunk.content, end=\"\", flush=True)\n```\n\n### Function Calling\n\n#### Decorator\n\n```python\nfrom langchain_core.messages import HumanMessage, ToolMessage\nfrom langchain_core.tools import tool\nfrom vmxai_langchain import ChatVMX\n\n\n@tool\ndef add(a: int, b: int) -> int:\n    \"\"\"Adds a and b.\n\n    Args:\n        a: first int\n        b: second int\n    \"\"\"\n    return a + b\n\n\n@tool\ndef multiply(a: int, b: int) -> int:\n    \"\"\"Multiplies a and b.\n\n    Args:\n        a: first int\n        b: second int\n    \"\"\"\n    return a * b\n\n\ntools = [add, multiply]\nllm = ChatVMX(\n    resource=\"default\",\n)\n\nllm_with_tools = llm.bind_tools(tools)\nquery = \"What is 3 * 12? Also, what is 11 + 49?\"\n\nmessages = [HumanMessage(query)]\nai_msg = llm_with_tools.invoke(messages)\nmessages.append(ai_msg)\n\nfor tool_call in ai_msg.tool_calls:\n    selected_tool = {\"add\": add, \"multiply\": multiply}[tool_call[\"name\"].lower()]\n    tool_output = selected_tool.invoke(tool_call[\"args\"])\n    messages.append(ToolMessage(tool_output, tool_call_id=tool_call[\"id\"]))\n\nprint(llm_with_tools.invoke(messages))\n```\n\n#### Pydantic\n\n```python\nfrom langchain_core.pydantic_v1 import BaseModel, Field\nfrom vmxai_langchain import ChatVMX\nfrom vmxai_langchain.output_parsers.tools import PydanticToolsParser\n\n\n# Note that the docstrings here are crucial, as they will be passed along\n# to the model along with the class name.\nclass add(BaseModel):\n    \"\"\"Add two integers together.\"\"\"\n\n    a: int = Field(..., description=\"First integer\")\n    b: int = Field(..., description=\"Second integer\")\n\n\nclass multiply(BaseModel):\n    \"\"\"Multiply two integers together.\"\"\"\n\n    a: int = Field(..., description=\"First integer\")\n    b: int = Field(..., description=\"Second integer\")\n\n\ntools = [add, multiply]\n\nllm = ChatVMX(\n    resource=\"default\",\n)\n\nllm_with_tools = llm.bind_tools(tools) | PydanticToolsParser(tools=[multiply, add])\n\nquery = \"What is 3 * 12? Also, what is 11 + 49?\"\n\nprint(llm_with_tools.invoke(query))\n\n```\n\n#### Function Calling Streaming\n\n```python\nfrom langchain_core.pydantic_v1 import BaseModel, Field\nfrom vmxai_langchain import ChatVMX\nfrom vmxai_langchain.output_parsers.tools import PydanticToolsParser\n\n\n# Note that the docstrings here are crucial, as they will be passed along\n# to the model along with the class name.\nclass add(BaseModel):\n    \"\"\"Add two integers together.\"\"\"\n\n    a: int = Field(..., description=\"First integer\")\n    b: int = Field(..., description=\"Second integer\")\n\n\nclass multiply(BaseModel):\n    \"\"\"Multiply two integers together.\"\"\"\n\n    a: int = Field(..., description=\"First integer\")\n    b: int = Field(..., description=\"Second integer\")\n\n\ntools = [add, multiply]\n\nllm = ChatVMX(\n    resource=\"default\",\n)\n\nllm_with_tools = llm.bind_tools(tools) | PydanticToolsParser(tools=[multiply, add])\n\nquery = \"What is 3 * 12? Also, what is 11 + 49?\"\n\nfor chunk in llm_with_tools.stream(query):\n    print(chunk)\n```\n\n### Structured Output\n\n```python\nfrom langchain_core.pydantic_v1 import BaseModel, Field\nfrom vmxai_langchain import ChatVMX\n\n\nclass Joke(BaseModel):\n    setup: str = Field(description=\"The setup of the joke\")\n    punchline: str = Field(description=\"The punchline to the joke\")\n\n\nllm = ChatVMX(resource=\"default\")\nstructured_llm = llm.with_structured_output(Joke, strict=True)\n\nprint(structured_llm.invoke(\"Tell me a joke about cats\"))\n\n```\n\n## Limitations\n\n1. Async client is not supported.\n2. `json_mode` and `json_schema` Structured output are not supported.\n\n## [Change Log](./CHANGELOG.md)\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "VM-X AI Langchain Python SDK",
    "version": "0.2.0",
    "project_urls": {
        "Homepage": "https://github.com/vm-x-ai/vm-x-ai-sdk",
        "Repository": "https://github.com/vm-x-ai/vm-x-ai-sdk"
    },
    "split_keywords": [
        "vm-x",
        " ai",
        " sdk",
        " python",
        " langchain"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "53dea063659119ac4c5b7437288180ffebfd95ef49fcac51b364fc5275ffe819",
                "md5": "f600301b9c654e5b52ec61a57f30a17a",
                "sha256": "14863455ab887ca1940d7a1bf13bf9124e1c945d77718b2dd5dd6a2fe1173bdb"
            },
            "downloads": -1,
            "filename": "vm_x_ai_langchain-0.2.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "f600301b9c654e5b52ec61a57f30a17a",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4,>=3.8.1",
            "size": 18483,
            "upload_time": "2024-08-23T16:32:31",
            "upload_time_iso_8601": "2024-08-23T16:32:31.440348Z",
            "url": "https://files.pythonhosted.org/packages/53/de/a063659119ac4c5b7437288180ffebfd95ef49fcac51b364fc5275ffe819/vm_x_ai_langchain-0.2.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "80ccabc76fbc1a2de83f4c2c44dc2f457dac6adb077caf2dd69ad6722761b888",
                "md5": "137c8f8d6ebe2dd7017e85b1a2afacfd",
                "sha256": "9ad58b483d09a1c88f57b03494a7a16a03c7fd484b6aa080979ae930c0959b49"
            },
            "downloads": -1,
            "filename": "vm_x_ai_langchain-0.2.0.tar.gz",
            "has_sig": false,
            "md5_digest": "137c8f8d6ebe2dd7017e85b1a2afacfd",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4,>=3.8.1",
            "size": 17692,
            "upload_time": "2024-08-23T16:32:32",
            "upload_time_iso_8601": "2024-08-23T16:32:32.318239Z",
            "url": "https://files.pythonhosted.org/packages/80/cc/abc76fbc1a2de83f4c2c44dc2f457dac6adb077caf2dd69ad6722761b888/vm_x_ai_langchain-0.2.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-08-23 16:32:32",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "vm-x-ai",
    "github_project": "vm-x-ai-sdk",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "vm-x-ai-langchain"
}
        
Elapsed time: 0.40737s