aigents


Nameaigents JSON
Version 0.9.0 PyPI version JSON
download
home_pagehttps://github.com/bessavagner/aigents.git
SummaryAdapters for Large Language Models and Generative Pre-trained Transformers APIs
upload_time2024-06-03 19:36:47
maintainerNone
docs_urlNone
authorVagner Bessa
requires_python<4.0,>=3.10
licenseGPL-3.0-only
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # aigents

Adapters for Large Language Models and Generative Pre-trained Transformers APIs

## Installation

### Using pip

```bash
python -m pip install aigents
```

## Usage

The `aigents` package intention is to tackle the issue of out there having a wide variety of different AI models APIs, even though most of them keep similar standards, it might be confusing or challeging for developers to merge, scale and write conssitent solutions that englobes them.

### Chat

The main classes for completions or chat have _Chatters_ on their name. For instance, `aigents.GoogleChatter` uses Google's AI completion models to generate chat responses. The method for calling responses is `answer`.

The constructors for _Chatters_ differs a little, but on thing in common on them is the `setup` (`str`) argument: a text instructing the AI model to play some role. For example:

``` python
from aigents import GoogleChatter

setup = "You are a very experienced and successful fantasy novel writer. "
## generate your API key: https://makersuite.google.com/app/apikey
api_key = "YOUR_GOOGLE_AI_API_KEY"
temperature = 0.5  # for creativity
chatter = GoogleChatter(setup=setup, api_key=api_key)

response = chatter.answer(
    "What are the difference between soft magic system "
    "and hard magic system?"
)
```

The returned value is a string corresponding on the first [candidate response from Gemini API](https://ai.google.dev/api/python/google/generativeai/GenerativeModel#generate_content), which you can access using the attribute `chatter.last_response`.

For `OpenAI` and `Google` chatters you can the the API key using `.env` file. Use the `.sample.env`: paste your API keys and save it as `.env`

#### Conversation

By default, `chatter.answer` has `conversation=True` for every _Chatter_. This means that the object keeps a record of the messages (conversation), which you can access using the attribute `chatter.messages`.

If the messages exceeds the token window allowed for the corresponding model, the instance will try to reduce the size of the messages. If the default values of `use_agent=True` is set for `chatter.answer`, the _Chatter_  will use another instance to summarize messages, in order to preserve most of the history and meaning of the conversation. Otherwise it will remove the oldest (but the `chatter.setup`, if provided) messages.

This section provides a brief overview of the classes in `core.py` and their usage.

#### Async version:

Every chatter have an async version. For instance, the previous code can be written using async version of the OpenAI's chatter:

``` python
from aigents import AsyncOpenAIChatter

setup = "You are a very experienced and successful fantasy novel writer. "
## generate your API key: https://platform.openai.com/api-keys
api_key = "YOUR_OPEN_AI_API_KEY"
temperature = 0.5  # for creativity
chatter = AsyncOpenAIChatter(setup=setup, api_key=api_key)

response = await chatter.answer(
    "What are the difference between soft magic system "
    "and hard magic system?"
)
```

#### Other Chatters:

* **AsyncGoogleChatter**: async version of `GoogleChatter`;
* **GoogleVision**: agent for image chat (<span style="color: #b04e27;">Important</span>: gemini-pro-vision does not generate images as for this date);
* **AsyncGoogleVision**: async version of `GoogleVision`
* **OpenAIChatter**: chatter that uses OpenAI's API;
* **BingChatter**: uses the uses [g4f](https://github.com/xtekky/gpt4free/tree/main) adpter, with `Bing` provider;
* **AsyncBingChatter**: async version of `BingChatter`.

#### Models

You can check different AI models used by `aigents` in module `constants`. For instance, for changing OpenAI chatter's model from `gpt-3.5-turbo-0125` to `gpt-4-0125-preview`:

```python
from aigents import OpenAIChatter
from aigents.constants import MODELS

api_key = "YOUR_OPEN_AI_API_KEY"
chatter = OpenAIChatter(setup=setup, api_key=api_key)
chatter.change_model(MODELS[3])
# always checked if it is the intended model
print(chatter.model)
```

### Context

The `Context` class in `aigents` provides functionality for generating embeddings from a source and generating a context based on a question. This is particularly useful for contextual chat applications.

#### Generating Embeddings

To generate embeddings from a source, you can use the `generate_embeddings` method. This method supports generating embeddings from a string, a pandas DataFrame, a Path to a parquet file containing embeddings, or a dictionary containing 'chunks'of text, 'n_tokens' corresponding to the number of tokens, counted using tiktoken library, and 'embeddings' which are numpy arrays of the encoded chunks as embeddings.

Here's an example of how to use the `generate_embeddings` method:

```python
import asyncio
from aigents import Context

async def get_embeddings_dataframe(text):
    
    # Initialize the Context with your text
    context = Context(text=text)
    # Generate embeddings using the default model
    return await context.generate_embeddings(embedding_model='gemini')

text = ''
with open('path/to/your/text', 'r') as textfile:
    text = textfile.read()
data = asyncio.run(get_embeddings_dataframe(text))
# Print the generated embeddings
print(data)
```

Here we use the Google's Gemini embedding generator. Note that you'll have to set your Gemini's API key in yur environment variables. Alternatively you can pass the api key as the argument `api_key` in `generate_embeddings` method.

Currently, embeddings generation via `Context` is only possible using async paradigm. This type of choice is due to the underperform of when using large texts, since each chunk of text corresponds to one api call, so it's better to make asynchronous calls.

After context generation, you can extract only relevant chunks based on a given *query*:


```python
import asyncio
from aigents import Context

async def get_relevant_chunks(question, text):
    embedding_model = 'gemini'
    pipeline = 'en_core_web_md'
    # Initialize the Context with your text
    context = Context(text=text)
    # Generate embeddings using the default model
    await context.generate_embeddings(embedding_model=embedding_model)
    return await context.generate_context(
        question,
        pipeline=pepiline
    )

text = ''
with open('path/to/your/text', 'r') as textfile:
    text = textfile.read()

question = 'What is this article about?'
relevant_data = asyncio.run(get_relevant_chunk(question, text))
# Print the generated embeddings
print(relevant_data)
```

You'll have to install the corresponding [spacy pipeline](https://github.com/explosion/spacy-models/releases). For instance, if your text is in portuguese, you'd want to install `pt-core-news-sm` or `pt-core-news-md`:

```bash
python -m spacy install pt-core-news-md
```

Than pass the value to the argument `pipeline` of the `generate_context` method.

## Contributing

Interested in contributing? Check out the contributing guidelines. Please note that this project is released with a Code of Conduct. By contributing to this project, you agree to abide by its terms.

## License

This project is licensed under [GNU_GPL_v3.0](https://github.com/xtekky/gpt4free/blob/main/LICENSE)

## Credits

`aigents` was created with [`cookiecutter`](https://cookiecutter.readthedocs.io/en/latest/) and the `py-pkgs-cookiecutter` [template](https://github.com/py-pkgs/py-pkgs-cookiecutter).

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/bessavagner/aigents.git",
    "name": "aigents",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.10",
    "maintainer_email": null,
    "keywords": null,
    "author": "Vagner Bessa",
    "author_email": "bessavagner@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/29/52/854632d1aa124641a63ae4f44aee580a5912b454ea6ad912e89deebef668/aigents-0.9.0.tar.gz",
    "platform": null,
    "description": "# aigents\n\nAdapters for Large Language Models and Generative Pre-trained Transformers APIs\n\n## Installation\n\n### Using pip\n\n```bash\npython -m pip install aigents\n```\n\n## Usage\n\nThe `aigents` package intention is to tackle the issue of out there having a wide variety of different AI models APIs, even though most of them keep similar standards, it might be confusing or challeging for developers to merge, scale and write conssitent solutions that englobes them.\n\n### Chat\n\nThe main classes for completions or chat have _Chatters_ on their name. For instance, `aigents.GoogleChatter` uses Google's AI completion models to generate chat responses. The method for calling responses is `answer`.\n\nThe constructors for _Chatters_ differs a little, but on thing in common on them is the `setup` (`str`) argument: a text instructing the AI model to play some role. For example:\n\n``` python\nfrom aigents import GoogleChatter\n\nsetup = \"You are a very experienced and successful fantasy novel writer. \"\n## generate your API key: https://makersuite.google.com/app/apikey\napi_key = \"YOUR_GOOGLE_AI_API_KEY\"\ntemperature = 0.5  # for creativity\nchatter = GoogleChatter(setup=setup, api_key=api_key)\n\nresponse = chatter.answer(\n    \"What are the difference between soft magic system \"\n    \"and hard magic system?\"\n)\n```\n\nThe returned value is a string corresponding on the first [candidate response from Gemini API](https://ai.google.dev/api/python/google/generativeai/GenerativeModel#generate_content), which you can access using the attribute `chatter.last_response`.\n\nFor `OpenAI` and `Google` chatters you can the the API key using `.env` file. Use the `.sample.env`: paste your API keys and save it as `.env`\n\n#### Conversation\n\nBy default, `chatter.answer` has `conversation=True` for every _Chatter_. This means that the object keeps a record of the messages (conversation), which you can access using the attribute `chatter.messages`.\n\nIf the messages exceeds the token window allowed for the corresponding model, the instance will try to reduce the size of the messages. If the default values of `use_agent=True` is set for `chatter.answer`, the _Chatter_  will use another instance to summarize messages, in order to preserve most of the history and meaning of the conversation. Otherwise it will remove the oldest (but the `chatter.setup`, if provided) messages.\n\nThis section provides a brief overview of the classes in `core.py` and their usage.\n\n#### Async version:\n\nEvery chatter have an async version. For instance, the previous code can be written using async version of the OpenAI's chatter:\n\n``` python\nfrom aigents import AsyncOpenAIChatter\n\nsetup = \"You are a very experienced and successful fantasy novel writer. \"\n## generate your API key: https://platform.openai.com/api-keys\napi_key = \"YOUR_OPEN_AI_API_KEY\"\ntemperature = 0.5  # for creativity\nchatter = AsyncOpenAIChatter(setup=setup, api_key=api_key)\n\nresponse = await chatter.answer(\n    \"What are the difference between soft magic system \"\n    \"and hard magic system?\"\n)\n```\n\n#### Other Chatters:\n\n* **AsyncGoogleChatter**: async version of `GoogleChatter`;\n* **GoogleVision**: agent for image chat (<span style=\"color: #b04e27;\">Important</span>: gemini-pro-vision does not generate images as for this date);\n* **AsyncGoogleVision**: async version of `GoogleVision`\n* **OpenAIChatter**: chatter that uses OpenAI's API;\n* **BingChatter**: uses the uses [g4f](https://github.com/xtekky/gpt4free/tree/main) adpter, with `Bing` provider;\n* **AsyncBingChatter**: async version of `BingChatter`.\n\n#### Models\n\nYou can check different AI models used by `aigents` in module `constants`. For instance, for changing OpenAI chatter's model from `gpt-3.5-turbo-0125` to `gpt-4-0125-preview`:\n\n```python\nfrom aigents import OpenAIChatter\nfrom aigents.constants import MODELS\n\napi_key = \"YOUR_OPEN_AI_API_KEY\"\nchatter = OpenAIChatter(setup=setup, api_key=api_key)\nchatter.change_model(MODELS[3])\n# always checked if it is the intended model\nprint(chatter.model)\n```\n\n### Context\n\nThe `Context` class in `aigents` provides functionality for generating embeddings from a source and generating a context based on a question. This is particularly useful for contextual chat applications.\n\n#### Generating Embeddings\n\nTo generate embeddings from a source, you can use the `generate_embeddings` method. This method supports generating embeddings from a string, a pandas DataFrame, a Path to a parquet file containing embeddings, or a dictionary containing 'chunks'of text, 'n_tokens' corresponding to the number of tokens, counted using tiktoken library, and 'embeddings' which are numpy arrays of the encoded chunks as embeddings.\n\nHere's an example of how to use the `generate_embeddings` method:\n\n```python\nimport asyncio\nfrom aigents import Context\n\nasync def get_embeddings_dataframe(text):\n    \n    # Initialize the Context with your text\n    context = Context(text=text)\n    # Generate embeddings using the default model\n    return await context.generate_embeddings(embedding_model='gemini')\n\ntext = ''\nwith open('path/to/your/text', 'r') as textfile:\n    text = textfile.read()\ndata = asyncio.run(get_embeddings_dataframe(text))\n# Print the generated embeddings\nprint(data)\n```\n\nHere we use the Google's Gemini embedding generator. Note that you'll have to set your Gemini's API key in yur environment variables. Alternatively you can pass the api key as the argument `api_key` in `generate_embeddings` method.\n\nCurrently, embeddings generation via `Context` is only possible using async paradigm. This type of choice is due to the underperform of when using large texts, since each chunk of text corresponds to one api call, so it's better to make asynchronous calls.\n\nAfter context generation, you can extract only relevant chunks based on a given *query*:\n\n\n```python\nimport asyncio\nfrom aigents import Context\n\nasync def get_relevant_chunks(question, text):\n    embedding_model = 'gemini'\n    pipeline = 'en_core_web_md'\n    # Initialize the Context with your text\n    context = Context(text=text)\n    # Generate embeddings using the default model\n    await context.generate_embeddings(embedding_model=embedding_model)\n    return await context.generate_context(\n        question,\n        pipeline=pepiline\n    )\n\ntext = ''\nwith open('path/to/your/text', 'r') as textfile:\n    text = textfile.read()\n\nquestion = 'What is this article about?'\nrelevant_data = asyncio.run(get_relevant_chunk(question, text))\n# Print the generated embeddings\nprint(relevant_data)\n```\n\nYou'll have to install the corresponding [spacy pipeline](https://github.com/explosion/spacy-models/releases). For instance, if your text is in portuguese, you'd want to install `pt-core-news-sm` or `pt-core-news-md`:\n\n```bash\npython -m spacy install pt-core-news-md\n```\n\nThan pass the value to the argument `pipeline` of the `generate_context` method.\n\n## Contributing\n\nInterested in contributing? Check out the contributing guidelines. Please note that this project is released with a Code of Conduct. By contributing to this project, you agree to abide by its terms.\n\n## License\n\nThis project is licensed under [GNU_GPL_v3.0](https://github.com/xtekky/gpt4free/blob/main/LICENSE)\n\n## Credits\n\n`aigents` was created with [`cookiecutter`](https://cookiecutter.readthedocs.io/en/latest/) and the `py-pkgs-cookiecutter` [template](https://github.com/py-pkgs/py-pkgs-cookiecutter).\n",
    "bugtrack_url": null,
    "license": "GPL-3.0-only",
    "summary": "Adapters for Large Language Models and Generative Pre-trained Transformers APIs",
    "version": "0.9.0",
    "project_urls": {
        "Homepage": "https://github.com/bessavagner/aigents.git",
        "Repository": "https://github.com/bessavagner/aigents.git"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "92272db4b6c5e12e2ff9c634a457fa1112c7b0ff7b9c188fe374a368956edd7c",
                "md5": "a95dd93ac3b4c4a0e2e0db0d41a52514",
                "sha256": "21d5e2ab564bda4884b6f4af5640f87d9a21c0ffa6b6656cd3492c46a938a96c"
            },
            "downloads": -1,
            "filename": "aigents-0.9.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "a95dd93ac3b4c4a0e2e0db0d41a52514",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.10",
            "size": 51534,
            "upload_time": "2024-06-03T19:36:45",
            "upload_time_iso_8601": "2024-06-03T19:36:45.260737Z",
            "url": "https://files.pythonhosted.org/packages/92/27/2db4b6c5e12e2ff9c634a457fa1112c7b0ff7b9c188fe374a368956edd7c/aigents-0.9.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2952854632d1aa124641a63ae4f44aee580a5912b454ea6ad912e89deebef668",
                "md5": "fe56ca2d933ec32b0365235923122003",
                "sha256": "22936fb4455ea06f3617f627bd9be15ebd05e483a75e97fe3d7abe87e937459b"
            },
            "downloads": -1,
            "filename": "aigents-0.9.0.tar.gz",
            "has_sig": false,
            "md5_digest": "fe56ca2d933ec32b0365235923122003",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.10",
            "size": 48184,
            "upload_time": "2024-06-03T19:36:47",
            "upload_time_iso_8601": "2024-06-03T19:36:47.203019Z",
            "url": "https://files.pythonhosted.org/packages/29/52/854632d1aa124641a63ae4f44aee580a5912b454ea6ad912e89deebef668/aigents-0.9.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-06-03 19:36:47",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "bessavagner",
    "github_project": "aigents",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "aigents"
}
        
Elapsed time: 0.25235s