Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement 'Get List' Functionality for Raven Bot to Fetch Document Lists from Frappe #1084

Merged
merged 6 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ export const FUNCTION_TYPES = [
requires_write_permissions: false,
type: "Bulk Operations"
},
// {
// value: "Get List",
// description: "Fetch a list of documents from the system (using filters).",
// requires_write_permissions: false,
// type: "Standard"
// },
{
value: "Get List",
description: "Fetch a list of documents from the system (using filters).",
requires_write_permissions: false,
type: "Standard"
},
{
value: "Create Document",
description: "Create any document in the system.",
Expand Down Expand Up @@ -109,4 +109,4 @@ export interface ArrayVariableType extends BaseVariableType {
type: 'array'
items: StringVariableType | NumberVariableType,
minItems?: number
}
}
13 changes: 13 additions & 0 deletions raven/ai/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,16 @@ def attach_file_to_document(doctype: str, document_id: str, file_path: str):
newFile.insert()

return {"document_id": document_id, "message": "File attached", "file_id": newFile.name}

def get_list(doctype: str, filters: dict = None, fields: list = None, limit: int = 20):
"""
Get a list of documents from the database
"""
if filters is None:
filters = {}

if fields is None:
fields = ["*"]

# Use the frappe.get_list method to get the list of documents
return frappe.get_list(doctype, filters=filters, fields=fields, limit=limit)
10 changes: 10 additions & 0 deletions raven/ai/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
get_documents,
update_document,
update_documents,
get_list
)
from raven.ai.openai_client import get_open_ai_client

Expand Down Expand Up @@ -195,6 +196,15 @@ def handle_requires_action(self, data, run_id):
self.publish_event(f"Attaching file to {doctype} {document_id}...")
function_output = attach_file_to_document(doctype, document_id, file_path)

if function.type == "Get List":
self.publish_event(f"Fetching list of {function.reference_doctype}...")
function_output = get_list(
function.reference_doctype,
filters=args.get("filters"),
fields=args.get("fields"),
limit=args.get("limit", 20)
)

tool_outputs.append(
{"tool_call_id": tool.id, "output": json.dumps(function_output, default=str)}
)
Expand Down
22 changes: 22 additions & 0 deletions raven/raven_ai/doctype/raven_ai_function/raven_ai_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,28 @@ def prepare_function_params(self):
}
elif self.type == "Custom Function":
params = json.loads(self.params)
elif self.type == "Get List":
params = {
"type": "object",
"properties": {
"filters": {
"type": "object",
"description": "Filters to apply when retrieving the list",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this need more details like "additional_properties" and the variable names?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we are currently working on this and testing different variations. We're exploring how best to handle additional_properties and variable names to ensure flexibility and accuracy. Thank you for the suggestion!

},
"fields": {
"type": "array",
"items": {"type": "string"},
"description": "Fields to retrieve for each document",
},
"limit": {
"type": "integer",
"description": "Maximum number of documents to retrieve",
"default": 20,
},
},
"required": ["filters", "fields"],
"additionalProperties": False,
}
else:
params = self.build_params_json_from_table()

Expand Down
Loading