Skip to content

Commit

Permalink
Address code review notes
Browse files Browse the repository at this point in the history
  • Loading branch information
pamella committed Jun 20, 2024
1 parent 89fcf86 commit d5d5ff1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 28 deletions.
13 changes: 7 additions & 6 deletions django_ai_assistant/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
from django_ai_assistant.models import Message, Thread


def init_api():
class API(NinjaAPI):
# Force "operationId" to be like "django_ai_assistant_delete_thread"
def get_openapi_operation_id(self, operation: Operation) -> str:
name = operation.view_func.__name__
return (package_name + "_" + name).replace(".", "_")
class API(NinjaAPI):
# Force "operationId" to be like "django_ai_assistant_delete_thread"
def get_openapi_operation_id(self, operation: Operation) -> str:
name = operation.view_func.__name__
return (package_name + "_" + name).replace(".", "_")


def init_api():
return API(
title=package_name,
version=version,
Expand Down
29 changes: 7 additions & 22 deletions docs/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ To create an AI Assistant, you need to:
```python title="myapp/ai_assistants.py"
from django_ai_assistant import AIAssistant


class WeatherAIAssistant(AIAssistant):
id = "weather_assistant"
name = "Weather Assistant"
Expand All @@ -50,12 +49,11 @@ such as getting the current date and finding the current weather by calling an A

Use the `@method_tool` decorator to define a tool method in the AI Assistant:

```{.python title="myapp/ai_assistants.py" hl_lines="15-22"}
```{.python title="myapp/ai_assistants.py" hl_lines="14-21"}
from django.utils import timezone
from django_ai_assistant import AIAssistant, method_tool
import json
class WeatherAIAssistant(AIAssistant):
id = "weather_assistant"
name = "Weather Assistant"
Expand Down Expand Up @@ -94,10 +92,9 @@ AI: The weather in NYC is sunny with a temperature of 25°C.

You have access to the current request user in tools:

```{.python title="myapp/ai_assistants.py" hl_lines=13}
```{.python title="myapp/ai_assistants.py" hl_lines=12}
from django_ai_assistant import AIAssistant, method_tool
class PersonalAIAssistant(AIAssistant):
id = "personal_assistant"
name = "Personal Assistant"
Expand All @@ -112,11 +109,10 @@ class PersonalAIAssistant(AIAssistant):

You can also add any Django logic to tools, such as querying the database:

```{.python title="myapp/ai_assistants.py" hl_lines=14-16}
```{.python title="myapp/ai_assistants.py" hl_lines=13-15}
from django_ai_assistant import AIAssistant, method_tool
import json
class IssueManagementAIAssistant(AIAssistant):
id = "issue_mgmt_assistant"
name = "Issue Management Assistant"
Expand Down Expand Up @@ -153,11 +149,10 @@ Then, set the `TAVILY_API_KEY` environment variable. You'll need to sign up at [

Finally, add the tool to your AI Assistant class by overriding the `get_tools` method:

```{.python title="myapp/ai_assistants.py" hl_lines="2 20"}
```{.python title="myapp/ai_assistants.py" hl_lines="2 19"}
from django_ai_assistant import AIAssistant
from langchain_community.tools.tavily_search import TavilySearchResults
class MovieSearchAIAssistant(AIAssistant):
id = "movie_search_assistant" # noqa: A003
instructions = (
Expand Down Expand Up @@ -191,7 +186,6 @@ You can manually call an AI Assistant from anywhere in your Django application:
```python
from myapp.ai_assistants import WeatherAIAssistant


assistant = WeatherAIAssistant()
output = assistant.run("What's the weather in New York City?")
assert output == "The weather in NYC is sunny with a temperature of 25°C."
Expand All @@ -210,11 +204,10 @@ and automatically retrieved then passed to the LLM when calling the AI Assistant

To create a `Thread`, you can use a helper from the `django_ai_assistant.use_cases` module. For example:

```{.python hl_lines="5 9"}
```{.python hl_lines="4 8"}
from django_ai_assistant.use_cases import create_thread, get_thread_messages
from myapp.ai_assistants import WeatherAIAssistant
thread = create_thread(name="Weather Chat", user=some_user)
assistant = WeatherAIAssistant()
assistant.run("What's the weather in New York City?", thread_id=thread.id)
Expand All @@ -233,7 +226,6 @@ such as a React application or a mobile app. Add the following to your Django pr
```python title="myproject/urls.py"
from django.urls import include, path


urlpatterns = [
path("ai-assistant/", include("django_ai_assistant.urls")),
...
Expand All @@ -259,7 +251,6 @@ The method signature for `AI_ASSISTANT_INIT_API_FN` is as follows:
```python
from ninja import NinjaAPI


def init_api():
return NinjaAPI(...)
```
Expand Down Expand Up @@ -293,7 +284,6 @@ Thread permission signatures look like this:
from django_ai_assistant.models import Thread
from django.http import HttpRequest


def check_custom_thread_permission(
thread: Thread,
user: Any,
Expand All @@ -307,7 +297,6 @@ While Message permission signatures look like this:
from django_ai_assistant.models import Thread, Message
from django.http import HttpRequest


def check_custom_message_permission(
message: Message,
thread: Thread,
Expand All @@ -327,7 +316,6 @@ but you can use [any chat model from Langchain that supports Tool Calling](https
from django_ai_assistant import AIAssistant
from langchain_anthropic import ChatAnthropic


class WeatherAIAssistant(AIAssistant):
id = "weather_assistant"
name = "Weather Assistant"
Expand All @@ -352,15 +340,13 @@ class WeatherAIAssistant(AIAssistant):
One AI Assistant can call another AI Assistant as a tool. This is useful for composing complex AI Assistants.
Use the `as_tool` method for that:

```{.python title="myapp/ai_assistants.py" hl_lines="14 16"}
```{.python title="myapp/ai_assistants.py" hl_lines="12 14"}
class SimpleAssistant(AIAssistant):
...
class AnotherSimpleAssistant(AIAssistant):
...
class ComplexAssistant(AIAssistant):
...
Expand Down Expand Up @@ -392,10 +378,9 @@ For this to work, your must do the following in your AI Assistant:

For example:

```{.python title="myapp/ai_assistants.py" hl_lines="12 16 18"}
```{.python title="myapp/ai_assistants.py" hl_lines="11 15 17"}
from django_ai_assistant import AIAssistant
class DocsAssistant(AIAssistant):
id = "docs_assistant" # noqa: A003
name = "Docs Assistant"
Expand Down

0 comments on commit d5d5ff1

Please sign in to comment.