The module defines a custom chat model that interacts with the Lilypad API to generate conversational responses and bind tools for enhanced functionality, with mechanisms for parsing tool calls and customizing message payloads.
Project description
ChatLilypad
ChatLilypad is a powerful and customizable chat model built on the LangChain framework. It integrates seamlessly with the Lilypad API, extending the functionality of traditional chat models through tool binding, structured output, and workflow integration. This module empowers developers to create dynamic, intelligent conversational agents tailored to their unique needs.
Overview
ChatLilypad is designed to facilitate intelligent, scalable conversations by combining a custom chat model with external tools via the Lilypad API. The model supports structured responses, tool binding, and graph and workflow integration while providing high configurability for advanced use cases.
Features
- Lilypad Chat Model: Dynamically generates responses using the Lilypad API.
- Tool Integration: Connects external tools to expand conversational capabilities.
- High Configurability: Allows developers to modify parameters like temperature, API endpoints, and tool configurations.
- Structured Output: Delivers organized, schema-defined responses for improved validation.
- Error Handling: Incorporates fallback mechanisms for API connectivity and parsing issues.
- Extendable Design: Built on
LangChain, enabling modular development and scalability.
Setup
Prerequisites
To use ChatLilypad, ensure you have:
- Python 3.8 or higher
- Pip (Python package manager)
- API key for the Lilypad API
Dependencies
Required Python packages:
requests: For HTTP API communication.json: For data serialization and deserialization.pydantic: For data validation and model schema definitions.langchain_core: Core classes and functionality for chat models.
Install all dependencies:
pip install requests pydantic langchain-core
Installation
To install the ChatLilypad module:
pip install langchain-lilypad
Usage
Initializing the Model
Import and initialize ChatLilypad with your Lilypad API key and the model name:
from langchain_lilypad import ChatLilypad
lilypad_model = ChatLilypad(
model_name="your_model_name",
api_key="your_api_key"
)
Generating Responses
Send a list of messages to the model to generate intelligent responses:
from langchain_core.messages import BaseMessage
messages = [
BaseMessage(type="human", content="Tell me a joke!"),
]
response = lilypad_model.invoke(messages)
print({"messages": [response]})
Customization
Adjusting Temperature
Control the randomness of responses by modifying the temperature parameter:
lilypad_model.temperature = 0.8
Changing the API Endpoint
Connect to a custom API endpoint by updating the api_url:
lilypad_model.api_url = "https://new-api-endpoint.com"
Tool Integration
Tooling enhances ChatLilypad's capabilities by enabling seamless integration with external APIs and resources.
Supported Models
Currently, only the following models support tooling:
- llama3.1:8b
- qwen2.5:7b
- qwen2.5-coder:7b
- phi4-mini:3.8b
- mistral:7b
Defining and Binding Tools
Tools can be defined and dynamically bound to the model. For example:
from langchain_community.tools import DuckDuckGoSearchResults
from langchain_core.tools import tool
search = DuckDuckGoSearchResults(safesearch="strict", max_results=10)
@tool
def websearch(webprompt: str) -> str:
"""Performs accurate web searches based on user input."""
return search.invoke(webprompt)
lilypad_model = lilypad_model.bind_tools([websearch])
Once bound, tools enable enhanced interaction during conversations.
Structured Output
ChatLilypad supports structured output for organized and schema-compliant responses.
Defining Structured Output
Define structured schemas using Pydantic models:
from pydantic import BaseModel, Field
from typing import Optional
class Joke(BaseModel):
"""Joke to tell user."""
setup: str = Field(description="The setup of the joke")
punchline: str = Field(description="The punchline of the joke")
rating: Optional[int] = Field(default=None, description="How funny the joke is (1-10)")
Example Usage
Bind the structured schema to the model:
structured_llm = lilypad_model.with_structured_output(Joke)
response = structured_llm.invoke("Tell me a joke")
print(response)
This ensures that generated responses conform to the Joke schema, improving validation and usability.
License
This project is licensed under the MIT License. See the LICENSE file for details.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file langchain_lilypad-0.1.4.tar.gz.
File metadata
- Download URL: langchain_lilypad-0.1.4.tar.gz
- Upload date:
- Size: 6.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e65a79da3d8912d7242da6729684af60c2584bb5b4ef47588bd345a2840e5726
|
|
| MD5 |
4ec38163c4acc64f11bcede0777d57fb
|
|
| BLAKE2b-256 |
54b0ef3bc14de596957be1d53b5807d1b647adb8a70d73b55241227d32a38ec2
|
File details
Details for the file langchain_lilypad-0.1.4-py3-none-any.whl.
File metadata
- Download URL: langchain_lilypad-0.1.4-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
41b7fff2bf0697a0e181f1d2ca66e68d855b929d4bd1bf7106b626480d9d5e23
|
|
| MD5 |
5d0fc56036a1851647ce5e64cc7b4dcd
|
|
| BLAKE2b-256 |
a325a731925610854ebf3f265d528e031ea6fa10420e42babec670a8ebbb30ed
|