[DOCS][How to Create A Custom Language Model]

pull/418/head
Kye 10 months ago
parent 780b04925e
commit fbba87060d

@ -154,6 +154,52 @@ print(out)
------
# `Agent` with Long Term Memory
`Agent` equipped with quasi-infinite long term memory. Great for long document understanding, analysis, and retrieval.
```python
from swarms import Agent, ChromaDB, OpenAIChat
# Making an instance of the ChromaDB class
memory = ChromaDB(
metric="cosine",
n_results=3,
output_dir="results",
docs_folder="docs",
)
# Initializing the agent with the Gemini instance and other parameters
agent = Agent(
agent_name="Covid-19-Chat",
agent_description=(
"This agent provides information about COVID-19 symptoms."
),
llm=OpenAIChat(),
max_loops="auto",
autosave=True,
verbose=True,
long_term_memory=memory,
stopping_condition="finish",
)
# Defining the task and image path
task = ("What are the symptoms of COVID-19?",)
# Running the agent with the specified task and image
out = agent.run(task)
print(out)
```
----
### `SequentialWorkflow`
Sequential Workflow enables you to sequentially execute tasks with `Agent` and then pass the output into the next agent and onwards until you have specified your max loops. `SequentialWorkflow` is wonderful for real-world business tasks like sending emails, summarizing documents, and analyzing data.

@ -0,0 +1,107 @@
# How to Create A Custom Language Model
When working with advanced language models, there might come a time when you need a custom solution tailored to your specific needs. Inheriting from an `AbstractLLM` in a Python framework allows developers to create custom language model classes with ease. This developer guide will take you through the process step by step.
### Prerequisites
Before you begin, ensure that you have:
- A working knowledge of Python programming.
- Basic understanding of object-oriented programming (OOP) in Python.
- Familiarity with language models and natural language processing (NLP).
- The appropriate Python framework installed, with access to `AbstractLLM`.
### Step-by-Step Guide
#### Step 1: Understand `AbstractLLM`
The `AbstractLLM` is an abstract base class that defines a set of methods and properties which your custom language model (LLM) should implement. Abstract classes in Python are not designed to be instantiated directly but are meant to be subclasses.
#### Step 2: Create a New Class
Start by defining a new class that inherits from `AbstractLLM`. This class will implement the required methods defined in the abstract base class.
```python
from swarms import AbstractLLM
class vLLMLM(AbstractLLM):
pass
```
#### Step 3: Initialize Your Class
Implement the `__init__` method to initialize your custom LLM. You'll want to initialize the base class as well and define any additional parameters for your model.
```python
class vLLMLM(AbstractLLM):
def __init__(self, model_name='default_model', tensor_parallel_size=1, *args, **kwargs):
super().__init__(*args, **kwargs)
self.model_name = model_name
self.tensor_parallel_size = tensor_parallel_size
# Add any additional initialization here
```
#### Step 4: Implement Required Methods
Implement the `run` method or any other abstract methods required by `AbstractLLM`. This is where you define how your model processes input and returns output.
```python
class vLLMLM(AbstractLLM):
# ... existing code ...
def run(self, task, *args, **kwargs):
# Logic for running your model goes here
return "Processed output"
```
#### Step 5: Test Your Model
Instantiate your custom LLM and test it to ensure that it works as expected.
```python
model = vLLMLM(model_name='my_custom_model', tensor_parallel_size=2)
output = model.run("What are the symptoms of COVID-19?")
print(output) # Outputs: "Processed output"
```
#### Step 6: Integrate Additional Components
Depending on the requirements, you might need to integrate additional components such as database connections, parallel computing resources, or custom processing pipelines.
#### Step 7: Documentation
Write comprehensive docstrings for your class and its methods. Good documentation is crucial for maintaining the code and for other developers who might use your model.
```python
class vLLMLM(AbstractLLM):
"""
A custom language model class that extends AbstractLLM.
... more detailed docstring ...
"""
# ... existing code ...
```
#### Step 8: Best Practices
Follow best practices such as error handling, input validation, and resource management to ensure your model is robust and reliable.
#### Step 9: Packaging Your Model
Package your custom LLM class into a module or package that can be easily distributed and imported into other projects.
#### Step 10: Version Control and Collaboration
Use a version control system like Git to track changes to your model. This makes collaboration easier and helps you keep a history of your work.
### Conclusion
By following this guide, you should now have a custom model that extends the `AbstractLLM`. Remember that the key to a successful custom LLM is understanding the base functionalities, implementing necessary changes, and testing thoroughly. Keep iterating and improving based on feedback and performance metrics.
### Further Reading
- Official Python documentation on abstract base classes.
- In-depth tutorials on object-oriented programming in Python.
- Advanced NLP techniques and optimization strategies for language models.
This guide provides the fundamental steps to create custom models using `AbstractLLM`. For detailed implementation and advanced customization, it's essential to dive deeper into the specific functionalities and capabilities of the language model framework you are using.

@ -71,6 +71,7 @@ nav:
- AbstractAgent: "swarms/agents/abstractagent.md"
- ToolAgent: "swarms/agents/toolagent.md"
- swarms.models:
- How to Create A Custom Language Model: "swarms/models/custom_model.md"
- Language:
- BaseLLM: "swarms/models/base_llm.md"
- Overview: "swarms/models/index.md"

@ -0,0 +1,89 @@
from vllm import LLM
from swarms import AbstractLLM, Agent, ChromaDB
# Making an instance of the VLLM class
class vLLMLM(AbstractLLM):
"""
This class represents a variant of the Language Model (LLM) called vLLMLM.
It extends the AbstractLLM class and provides additional functionality.
Args:
model_name (str): The name of the LLM model to use. Defaults to "acebook/opt-13b".
tensor_parallel_size (int): The size of the tensor parallelism. Defaults to 4.
*args: Variable length argument list.
**kwargs: Arbitrary keyword arguments.
Attributes:
model_name (str): The name of the LLM model.
tensor_parallel_size (int): The size of the tensor parallelism.
llm (LLM): An instance of the LLM class.
Methods:
run(task: str, *args, **kwargs): Runs the LLM model to generate output for the given task.
"""
def __init__(
self,
model_name: str = "acebook/opt-13b",
tensor_parallel_size: int = 4,
*args,
**kwargs
):
super().__init__(*args, **kwargs)
self.model_name = model_name
self.tensor_parallel_size = tensor_parallel_size
self.llm = LLM(
model_name=self.model_name,
tensor_parallel_size=self.tensor_parallel_size,
)
def run(self, task: str, *args, **kwargs):
"""
Runs the LLM model to generate output for the given task.
Args:
task (str): The task for which to generate output.
*args: Variable length argument list.
**kwargs: Arbitrary keyword arguments.
Returns:
str: The generated output for the given task.
"""
return self.llm.generate(task)
# Initializing the agent with the vLLMLM instance and other parameters
model = vLLMLM(
"facebook/opt-13b",
tensor_parallel_size=4,
)
# Defining the task
task = "What are the symptoms of COVID-19?"
# Running the agent with the specified task
out = model.run(task)
# Integrate Agent
agent = Agent(
agent_name="Doctor agent",
agent_description=(
"This agent provides information about COVID-19 symptoms."
),
llm=model,
max_loops="auto",
autosave=True,
verbose=True,
long_term_memory=ChromaDB(
metric="cosine",
n_results=3,
output_dir="results",
docs_folder="docs",
),
stopping_condition="finish",
)
Loading…
Cancel
Save