Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
schleising committed Aug 20, 2023
1 parent 74bcc33 commit 97e22de
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 11 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ The output of the functions is a [SimpleOpenaiResponse](https://schleising.githu
- `success` - A boolean indicating whether the request was successful or not.
- `message` - The message returned by the API.

### Functions

Functions can be added to the client using the `add_function` method. This method takes a function name and a function as arguments. The function should take an [OpenAIFunction](https://schleising.github.io/simple-openai/simple_openai/public_models/#src.simple_openai.models.open_ai_models.OpenAIFunction) object as its first argument, and the Python function itself as the second argument.

The Python function should return a string, which will be passed to the API using the `function` role

## Documentation

The documentation is available on [GitHub](https://schleising.github.io/simple-openai/)
6 changes: 6 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ The output of the functions is a [SimpleOpenaiResponse](simple_openai/responses.
- `success` - A boolean indicating whether the request was successful or not.
- `message` - The message returned by the API.

### Functions

Functions can be added to the client using the `add_function` method. This method takes a function name and a function as arguments. The function should take an [OpenAIFunction](simple_openai/public_models.md/#src.simple_openai.models.open_ai_models.OpenAIFunction) object as its first argument, and the Python function itself as the second argument.

The Python function should return a string, which will be passed to the API using the `function` role

## Documentation

The documentation for the package can be found in the reference section.
1 change: 1 addition & 0 deletions docs/simple_openai/function_manager.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:::src.simple_openai.function_manager
1 change: 1 addition & 0 deletions docs/simple_openai/public_models.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:::src.simple_openai.models.open_ai_models
2 changes: 2 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,5 @@ nav:
- AsyncSimpleOpenai: simple_openai/async_simple_openai.md
- SimpleOpenaiResponse: simple_openai/responses.md
- ChatManager: simple_openai/chat_manager.md
- FunctionManager: simple_openai/function_manager.md
- Public Models: simple_openai/public_models.md
33 changes: 23 additions & 10 deletions src/simple_openai/function_manager.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
from typing import Callable
"""This module contains the function manager.
The function manager is used to manage the functions that can be called by the bot.
Define a function using the [OpenAIFunction](public_models.md/#src.simple_openai.models.open_ai_models.OpenAIFunction) model from models.py and add it to the function manager using the add_function method.
The function should return a string.
The function can optionally take keyword arguments, the keyword arguments should be defined in the OpenAIFunction model.
Call the function using the call_function method for synchronous functions or the async_call_function method for asynchronous functions.
"""

from typing import Any, Callable
from dataclasses import dataclass

from .models import open_ai_models
Expand All @@ -10,7 +23,7 @@ class OpenAIFunctionMapping:
This class represents an OpenAI function mapping.
Args:
function_definition (open_ai_models.OpenAIFunction): The description of the function
function_definition (OpenAIFunction): The description of the function
function (Callable): The function to call
"""
function_definition: open_ai_models.OpenAIFunction
Expand All @@ -19,27 +32,27 @@ class OpenAIFunctionMapping:
class FunctionManager:
"""Function manager
This class manages the functions that can be called by the chat manager.
This class manages the functions that can be called by the bot.
"""

def __init__(self) -> None:
self._functions: dict[str, OpenAIFunctionMapping] = {}

def add_function(self, function_definition: open_ai_models.OpenAIFunction, function: Callable) -> None:
"""Add a function to the chat manager
"""Add a function to the function manager
Args:
function_definition (open_ai_models.OpenAIFunction): The function definition
function_definition (OpenAIFunction): The function definition
function (Callable): The function to call
"""
# Add the function to the chat manager
# Add the function to the function manager
self._functions[function_definition.name] = OpenAIFunctionMapping(function_definition, function)

def get_json_function_list(self) -> list[open_ai_models.OpenAIFunction] | None:
"""Get the list of functions as JSON
"""Get the list of functions
Returns:
list[str]: The list of functions as JSON
list[open_ai_models.OpenAIFunction] | None: The list of functions or None if there are no functions
"""
# Get the list of functions
functions = [function.function_definition for function in self._functions.values()]
Expand All @@ -51,7 +64,7 @@ def get_json_function_list(self) -> list[open_ai_models.OpenAIFunction] | None:
# Return None
return None

def call_function(self, function_name: str, **kwargs) -> str:
def call_function(self, function_name: str, **kwargs: dict[str, Any]) -> str:
"""Call a function
Args:
Expand All @@ -69,7 +82,7 @@ def call_function(self, function_name: str, **kwargs) -> str:
# Call the function
return self._functions[function_name].function(**kwargs)

async def async_call_function(self, function_name: str, **kwargs) -> str:
async def async_call_function(self, function_name: str, **kwargs: dict[str, Any]) -> str:
"""Call a function
Args:
Expand Down
Empty file.
35 changes: 35 additions & 0 deletions src/simple_openai/models/open_ai_models.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,50 @@
""" OpenAI API models
This module contains the models for the OpenAI API.
The models are used to validate the data sent to and received from the OpenAI API.
The models are based on the [OpenAI API documentation](https://beta.openai.com/docs/api-reference/introduction) and use [Pydantic](https://pydantic-docs.helpmanual.io/) to help serialise and deserialise the JSON.
"""

from pydantic import BaseModel

class OpenAIParameter(BaseModel):
""" OpenAI parameter
This class represents an OpenAI parameter.
Attributes:
type (str): The type of the parameter
description (str): The description of the parameter, used by OpenAI to decide whether to use the parameter
"""
type: str
description: str

class OpenAIParameters(BaseModel):
""" OpenAI parameters
This class represents a list of OpenAI parameters.
Attributes:
type (str): The type of the parameters
properties (dict[str, OpenAIParameter]): The parameters
required (list[str], optional): The required parameters. Defaults to [].
"""
type: str = 'object'
properties: dict[str, OpenAIParameter]
required: list[str] = []

class OpenAIFunction(BaseModel):
""" OpenAI function
This class represents an OpenAI function.
Attributes:
name (str): The name of the function
description (str): The description of the function, used by OpenAI to decide whether to use the function
parameters (OpenAIParameters): The parameters of the function
"""
name: str
description: str
parameters: OpenAIParameters
Expand Down
2 changes: 1 addition & 1 deletion src/simple_openai/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class SimpleOpenaiResponse:
If `success` is False, `message` will contain the error message.
Args:
Attributes:
success (bool): True if the request was successful, False otherwise
message (str): The message of the response
"""
Expand Down

0 comments on commit 97e22de

Please sign in to comment.