From 41a53976ffdf35c008c956dea7a23edb93d38d15 Mon Sep 17 00:00:00 2001 From: bgodlin <37313677+bgodlin@users.noreply.github.com> Date: Tue, 25 Jun 2024 15:16:49 +0000 Subject: [PATCH] Added a test to verify the successful query response analysis --- .../__init__.py | 0 .../component.yaml | 4 +-- .../graphql_response_analyser.py} | 5 +-- tests/test_tools.py | 36 +++++++++++++++++++ tox.ini | 2 +- 5 files changed, 42 insertions(+), 5 deletions(-) rename packages/subquery/customs/{graphql-data-fetching => graphql_response_analyser}/__init__.py (100%) rename packages/subquery/customs/{graphql-data-fetching => graphql_response_analyser}/component.yaml (82%) rename packages/subquery/customs/{graphql-data-fetching/graphql-response-analyser.py => graphql_response_analyser/graphql_response_analyser.py} (91%) diff --git a/packages/subquery/customs/graphql-data-fetching/__init__.py b/packages/subquery/customs/graphql_response_analyser/__init__.py similarity index 100% rename from packages/subquery/customs/graphql-data-fetching/__init__.py rename to packages/subquery/customs/graphql_response_analyser/__init__.py diff --git a/packages/subquery/customs/graphql-data-fetching/component.yaml b/packages/subquery/customs/graphql_response_analyser/component.yaml similarity index 82% rename from packages/subquery/customs/graphql-data-fetching/component.yaml rename to packages/subquery/customs/graphql_response_analyser/component.yaml index bda93389..ec9f52b1 100644 --- a/packages/subquery/customs/graphql-data-fetching/component.yaml +++ b/packages/subquery/customs/graphql_response_analyser/component.yaml @@ -1,11 +1,11 @@ -name: graphql-response-analyser +name: graphql_response_analyser author: subquery version: 0.1.0 type: custom description: This module explains the output of the GraphQL server within a given context, including a description of the indexed data being served and the query itself. license: Apache-2.0 aea_version: '>=1.0.0, <2.0.0' -entry_point: graphql-response-analyser.py +entry_point: graphql_response_analyser.py callable: run dependencies: openai: diff --git a/packages/subquery/customs/graphql-data-fetching/graphql-response-analyser.py b/packages/subquery/customs/graphql_response_analyser/graphql_response_analyser.py similarity index 91% rename from packages/subquery/customs/graphql-data-fetching/graphql-response-analyser.py rename to packages/subquery/customs/graphql_response_analyser/graphql_response_analyser.py index 024abc27..c6abe3c5 100644 --- a/packages/subquery/customs/graphql-data-fetching/graphql-response-analyser.py +++ b/packages/subquery/customs/graphql_response_analyser/graphql_response_analyser.py @@ -20,7 +20,6 @@ """This module accepts a GraphQL endpoint, executes queries based on a given description, and explains the response in natural language""" from typing import Any, Dict, Optional, Tuple -import os from openai import OpenAI import json import requests @@ -32,7 +31,7 @@ def analyse_data_and_generate_response(description, query, data): return f""" - You're a GraphQL query response analyzer. You will be provided with context about the data served by the endpoint, as well as the executed query, to give you a better understanding. Based on this, you are expected to return a short bullet point description of the response in natural language. + You're a GraphQL query response analyzer. You will be provided with context about the data served by the endpoint, as well as the executed query, to give you a better understanding. Description: @@ -46,6 +45,8 @@ def analyse_data_and_generate_response(description, query, data): {json.dumps(data)} + Based on the provided context, please generate a bullet-pointed summary in a machine-readable JSON format. The JSON structure should have an array object named 'analysis_result,' with each analytical conclusion represented as a separate string element within the array. + """ diff --git a/tests/test_tools.py b/tests/test_tools.py index bb0c7882..45a22b6d 100644 --- a/tests/test_tools.py +++ b/tests/test_tools.py @@ -31,6 +31,9 @@ from packages.valory.customs.prediction_request import prediction_request from packages.valory.skills.task_execution.utils.apis import KeyChain from packages.valory.skills.task_execution.utils.benchmarks import TokenCounterCallback + +from packages.subquery.customs.graphql_response_analyser import graphql_response_analyser + from tests.constants import ( OPENAI_SECRET_KEY, STABILITY_API_KEY, @@ -175,3 +178,36 @@ def _validate_response(self, response: Any) -> None: super()._validate_response(response) expected_num_tx_params = 2 assert len(response[2].keys()) == expected_num_tx_params + +test_query = """ +{ + transfers { + aggregates { + keys + sum { + value + } + } + } +} +""" + +class TestGraphResponseAnalyser(): + """Check successful query output analysis""" + + tool_callable: str = "run" + tool_module = graphql_response_analyser + + def test_run(self) -> None: + """Test run method.""" + kwargs = dict( + tool = "openai-gpt-3.5-turbo", + request="When was the first transfer?", + query = test_query, + endpoint="https://api.subquery.network/sq/subquery/cusdnew__c3Vic", + description="This project manages and indexes data pertaining to cUSD (CELO USD) ERC-20 token transfers and approvals recorded within a dedicated smart contract. The stored data includes information on approvals granted and transfers executed. These entities provide insights into the authorization and movement of USDT tokens within the CELO ecosystem, facilitating analysis and monitoring of token transactions.", + api_keys={"openai": OPENAI_SECRET_KEY} + ) + func = getattr(self.tool_module, self.tool_callable) + response = func(**kwargs) + assert "analysis_result" in response[0] \ No newline at end of file diff --git a/tox.ini b/tox.ini index b38610c0..6e9f2175 100644 --- a/tox.ini +++ b/tox.ini @@ -20,7 +20,7 @@ deps = {[deps-tests]deps} open-autonomy==0.14.10 fastapi==0.110.3 - openai==0.27.2 + openai==1.11.0 requests==2.28.1 mech-client==0.2.5 py-multibase==1.0.3