Skip to content

Commit

Permalink
Merge pull request #1084 from bvisible/get_list
Browse files Browse the repository at this point in the history
Implement 'Get List' Functionality for Raven Bot to Fetch Document Lists from Frappe
  • Loading branch information
nikkothari22 authored Oct 11, 2024
2 parents 4e3a54a + 11f830a commit f041817
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
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",
},
"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

0 comments on commit f041817

Please sign in to comment.