Skip to content

Commit

Permalink
fixes conflicts on poetry.lock
Browse files Browse the repository at this point in the history
  • Loading branch information
filipeximenes committed Aug 27, 2024
2 parents eab9d96 + bcb92af commit 07d38f8
Show file tree
Hide file tree
Showing 21 changed files with 424 additions and 277 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name: Publish packages (PyPI, npm)
on:
release:
types: [published]
workflow_dispatch:

jobs:
publish-pypi:
Expand Down Expand Up @@ -71,6 +72,6 @@ jobs:
- name: Publish package distributions to npm
working-directory: ./frontend
run: |
pnpm publish
pnpm publish --no-git-checks
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_API_TOKEN }}
2 changes: 1 addition & 1 deletion .python-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.12.3
3.12.4
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ frontend TypeScript client, `django-ai-assistant-client`.
The backend and the frontend are versioned together, that is, they have the same version number.
When you update the backend, you should also update the frontend to the same version.

## 0.0.4 <small>July 5, 2024</small> {id="0.0.4"}

- Fix frontend README.

## 0.0.3 <small>July 5, 2024</small> {id="0.0.3"}

- Less restrictive Python version in pyproject.toml. Support future Python versions.

## 0.0.2 <small>June 28, 2024</small> {id="0.0.2"}

- Add support for Django 4.2 LTS
Expand Down
53 changes: 42 additions & 11 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,37 @@ Please follow this guide to learn more about how to develop and test the project
git clone [email protected]:vintasoftware/django-ai-assistant.git
```

### Set up a virtualenv, optionally set up nvm, and activate your environment(s)
### Install development tools

You can use [pyenv](https://github.com/pyenv/pyenv), [pipenv](https://github.com/pypa/pipenv/blob/main/docs/installation.md), vanilla venvs or the tool of your choice.
This project uses [Poetry](https://python-poetry.org/docs/) for dependency and virtual environment management.

If you need to install the version of Python recommended for the project, you can use [Pyenv](https://github.com/pyenv/pyenv).

For installing Node, we recommend [NVM](https://github.com/nvm-sh/nvm).

### Install dependencies

#### Backend

Go to the project root and install the Python dependencies:
Go to the project root. To instantiate the virtual environment, run

```bash
poetry shell
```

Install the Python dependencies:

```bash
poetry install
```

If you encounter an error regarding the Python version required for the project, you can use pyenv to install the appropriate version based on [.python-version](.python-version):

```bash
pyenv install
```


#### Frontend

Go to the frontend directory and install the Node dependencies:
Expand Down Expand Up @@ -68,6 +83,12 @@ Then follow the instructions in the [example README](https://github.com/vintasof

## Tests

Before running tests copy the `.env.example` file to `.env.tests`.

```bash
cp .env.example .env.tests
```

Run tests with:

```bash
Expand All @@ -76,7 +97,8 @@ poetry run pytest

The tests use `pytest-vcr` to record and replay HTTP requests to AI models.

If you're implementing a new test that needs to call a real AI model, you need to set the `OPENAI_API_KEY` environment variable on root `.env` file.
If you're implementing a new test that needs to call a real AI model, you need to set the `OPENAI_API_KEY` environment variable with a real API key in the `.env.tests` file.

Then, you will run the tests in record mode:

```bash
Expand All @@ -102,10 +124,19 @@ poetry run mkdocs serve
To release and publish a new version, follow these steps:

1. Update the version in `pyproject.toml` and `frontend/package.json`.
2. In the project root, run `poetry run python manage.py generate_openapi_schema --output frontend/openapi_schema.json` to update the OpenAPI schema.
3. In the frontend directory, run `pnpm run generate-client` to update the TypeScript client with the new OpenAPI schema.
4. Update the changelog in `CHANGELOG.md`.
5. Open a PR with the changes.
6. Once the PR is merged, run the [Release GitHub Action](https://github.com/vintasoftware/django-ai-assistant/actions/workflows/release.yml) to create a draft release.
7. Review the draft release, ensure the description has at least the associated changelog entry, and publish it.
8. Once the review is publish, the [Publish GitHub Action](https://github.com/vintasoftware/django-ai-assistant/actions/workflows/publish.yml) will automatically run to publish the new version to [PyPI](https://pypi.org/project/django-ai-assistant) and [npm](https://www.npmjs.com/package/django-ai-assistant-client). Check the logs to ensure the publication was successful.
2. Re-install the local version of the Python project: `poetry install`
3. In the project root, run `poetry run python manage.py generate_openapi_schema --output frontend/openapi_schema.json` to update the OpenAPI schema.
4. Re-install the local version of the frontend project:

```bash
cd frontend
pnpm install
pnpm run build
```

5. In the frontend directory, run `pnpm run generate-client` to update the TypeScript client with the new OpenAPI schema.
6. Update the changelog in `CHANGELOG.md`.
7. Open a PR with the changes.
8. Once the PR is merged, run the [Release GitHub Action](https://github.com/vintasoftware/django-ai-assistant/actions/workflows/release.yml) to create a draft release.
9. Review the draft release, ensure the description has at least the associated changelog entry, and publish it.
10. Once the review is published, the [Publish GitHub Action](https://github.com/vintasoftware/django-ai-assistant/actions/workflows/publish.yml) will automatically run to publish the new version to [PyPI](https://pypi.org/project/django-ai-assistant) and [npm](https://www.npmjs.com/package/django-ai-assistant-client). Check the logs to ensure the publication was successful.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
[![CI](https://github.com/vintasoftware/django-ai-assistant/actions/workflows/ci.yml/badge.svg)](https://github.com/vintasoftware/django-ai-assistant/actions/workflows/ci.yml)
[![Coverage Status](https://coveralls.io/repos/github/vintasoftware/django-ai-assistant/badge.svg?branch=main)](https://coveralls.io/github/vintasoftware/django-ai-assistant?branch=main)
[![Discord Server](https://img.shields.io/discord/1260577482122203206)](https://discord.gg/mqdubnPb)

# django-ai-assistant <img src="https://raw.githubusercontent.com/vintasoftware/django-ai-assistant/main/docs/images/robot-happy-outline.svg" width="20" alt="robot logo">
# django-ai-assistant

<img align="left" src="https://raw.githubusercontent.com/vintasoftware/django-ai-assistant/main/docs/images/favicon.svg" height="120" alt="robot logo">

Combine the power of LLMs with Django's productivity to build intelligent applications.
Let AI Assistants call methods from Django's side and do anything your users need!

Use AI Tool Calling and RAG with Django to easily build state of the art AI Assistants.

⚠️ Under heavy development. Not publicly launched yet!

Please check the documentation: [https://vintasoftware.github.io/django-ai-assistant/](https://vintasoftware.github.io/django-ai-assistant/)

## 5 minute demo

https://github.com/vintasoftware/django-ai-assistant/assets/397989/715d7608-e657-4dce-a7be-2909679f814b

## Community

Feel free to ask questions and share your work on our [Discord Server](https://discord.gg/fQfH9PkJM6)!

## Contributing

You're welcome to contribute with Django AI Assistant! Please feel free to tackle existing [issues](https://github.com/vintasoftware/django-ai-assistant/issues). If you have a new idea, please create a thread on [Discussions](https://github.com/vintasoftware/django-ai-assistant/discussions).
Expand Down
41 changes: 40 additions & 1 deletion django_ai_assistant/admin.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,58 @@
from typing import ClassVar, List, Type

from django.contrib import admin
from django.contrib.admin.options import InlineModelAdmin
from django.urls import reverse
from django.utils.safestring import mark_safe

from django_ai_assistant.models import Message, Thread


class MessageInline(admin.TabularInline):
model = Message
extra = 0
fields = ("pk", "message_type", "content", "created_at")
readonly_fields = fields
ordering = ("created_at",)
show_change_link = True

def pk(self, obj):
display_text = "<a href={}>{}</a>".format(
reverse(
f"admin:{Message._meta.app_label}_{Message._meta.model_name}_change", args=(obj.pk,)
),
obj.pk,
)
return mark_safe(display_text) # noqa: S308

def message_type(self, obj):
return obj.message.get("type") if obj.message else None

def content(self, obj):
return obj.message.get("data", {}).get("content") if obj.message else None

def has_add_permission(self, request, obj=None):
return False

def has_delete_permission(self, request, obj=None):
return False

def has_change_permission(self, request, obj=None):
return False


@admin.register(Thread)
class ThreadAdmin(admin.ModelAdmin):
list_display = ("name", "created_at", "created_by", "updated_at")
search_fields = ("name",)
list_filter = ("created_at", "updated_at")
raw_id_fields = ("created_by",)
inlines: ClassVar[List[Type[InlineModelAdmin]]] = [MessageInline]


@admin.register(Message)
class MessageAdmin(admin.ModelAdmin):
list_display = ("id", "thread", "created_at", "created_at")
list_display = ("id", "thread", "created_at")
search_fields = ("thread__name", "message")
list_filter = ("created_at",)
raw_id_fields = ("thread",)
11 changes: 8 additions & 3 deletions django_ai_assistant/helpers/assistants.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
DEFAULT_DOCUMENT_PROMPT,
DEFAULT_DOCUMENT_SEPARATOR,
)
from langchain_core.chat_history import BaseChatMessageHistory, InMemoryChatMessageHistory
from langchain_core.chat_history import (
BaseChatMessageHistory,
InMemoryChatMessageHistory,
)
from langchain_core.language_models import BaseChatModel
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import (
Expand Down Expand Up @@ -295,7 +298,9 @@ def get_message_history(self, thread_id: Any | None) -> BaseChatMessageHistory:
"""

# DjangoChatMessageHistory must be here because Django may not be loaded yet elsewhere:
from django_ai_assistant.langchain.chat_message_histories import DjangoChatMessageHistory
from django_ai_assistant.langchain.chat_message_histories import (
DjangoChatMessageHistory,
)

if thread_id is None:
return InMemoryChatMessageHistory()
Expand Down Expand Up @@ -538,7 +543,7 @@ def invoke(self, *args: Any, thread_id: Any | None, **kwargs: Any) -> dict:
return chain.invoke(*args, **kwargs)

@with_cast_id
def run(self, message: str, thread_id: Any | None, **kwargs: Any) -> str:
def run(self, message: str, thread_id: Any | None = None, **kwargs: Any) -> str:
"""Run the assistant with the given message and thread ID.\n
This is the higher-level method to run the assistant.\n
Expand Down
9 changes: 9 additions & 0 deletions docs/images/favicon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions docs/images/logo-black-transparent-bg.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 07d38f8

Please sign in to comment.