Giter Club home page Giter Club logo

Comments (13)

Archetype90 avatar Archetype90 commented on August 14, 2024 2

Brilliant, this has done it. Great job, thank you very much for your help.

I would love to see this very simple, clean example make it into your documentation.

from python-dialogflow.

galz10 avatar galz10 commented on August 14, 2024 1

I have some great news! So after tackling a somewhat similar approach i believe i got the export and restore to work locally.

def restore_agent(agent_content,project_id):
    agent_client = AgentsClient()
    
    request = {
        "parent": "projects/"+project_id,
        "agent_content": agent_content
    }

    operation= agent_client.restore_agent(request=request)
    result = operation.result(timeout=300)    
    print(f"Restore result: {result}")


def export_agent(agent_name,project_id):
    agent_client = AgentsClient()

    exported_agent_name = agent_name

    operation = agent_client.export_agent(parent="projects/"+project_id,)

    result = operation.result(timeout=300)    
    restore_agent(result.agent_content,"project-id")



def main():
    export_agent("Agent-name","project-id")
if __name__ == "__main__":
    main()

Here's the code, again i would still suggest using the GCS Bucket but this should also work.
Again if you don't have any other issues then i'll close this issue.

from python-dialogflow.

galz10 avatar galz10 commented on August 14, 2024

Hi @Archetype90, this Dialogflow version is not the most recent/unsupported have you tried using Dialogflow V2 with exporting the agent ?

from python-dialogflow.

Archetype90 avatar Archetype90 commented on August 14, 2024

Hi @Archetype90, this Dialogflow version is not the most recent/unsupported have you tried using Dialogflow V2 with exporting the agent ?

I just spent the time to upgrade to Dialogflow V2.7.1, but I am still seeing the same issue.

Updated V2 Code Example

    def export_agent_callback(self, operation_future):
        if operation_future.operation.error.code == 0:
            self.logger.info("Agent exported successfully")
            parent = self.agent_client.common_project_path(self.project_id)
            response = self.agent_client.restore_agent(request={"parent": parent,
                                                                "agent_content": operation_future.operation.response.value})
            response.add_done_callback(self.restore_agent_callback)
        else:
            self.logger.error(f"Error Exporting Agent: {operation_future.operation.error}")

    def export_agent(self, agent_name):
        parent = self.agent_client.common_project_path(self.project_id)

        try:
            self.exported_agent_name = agent_name
            response = self.agent_client.export_agent(parent=parent)
            response.add_done_callback(self.export_agent_callback)
        except google_exceptions.GoogleAPICallError as ex:
            self.logger.error(ex)
        except google_exceptions.RetryError as ex:
            self.logger.error(ex)
        except ValueError as ex:
            self.logger.error(ex)

Updated V2 Stack Trace

2021-08-10 07:42:58 P0375 dialogflow[20148] INFO Agent exported successfully
Error while executing Future callback.
Traceback (most recent call last):
  File "C:\Projects\test\dialogflow\venv\lib\site-packages\google\api_core\grpc_helpers.py", line 67, in error_remapped_callable
    return callable_(*args, **kwargs)
  File "C:\Projects\test\dialogflow\venv\lib\site-packages\grpc\_channel.py", line 826, in __call__
    return _end_unary_response_blocking(state, call, False, None)
  File "C:\Projects\test\dialogflow\venv\lib\site-packages\grpc\_channel.py", line 729, in _end_unary_response_blocking
    raise _InactiveRpcError(state)
grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
	status = StatusCode.INVALID_ARGUMENT
	details = "com.google.apps.framework.request.BadRequestException: Invalid agent zip. Missing required json file agent.json"
	debug_error_string = "{"created":"@1628599378.363000000","description":"Error received from peer ipv4:xxx:443","file":"src/core/lib/surface/call.cc","file_line":1062,"grpc_message":"com.google.apps.framework.request.BadRequestException: Invalid agent zip. Missing required json file agent.json","grpc_status":3}"
>

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Projects\test\dialogflow\venv\lib\site-packages\google\api_core\future\_helpers.py", line 37, in safe_invoke_callback
    return callback(*args, **kwargs)
  File "C:\Projects\test\dialogflow\src\dialogflow.py", line 488, in export_agent_callback
    "agent_content": operation_future.operation.response.value})
  File "C:\Projects\test\dialogflow\venv\lib\site-packages\google\cloud\dialogflow_v2\services\agents\client.py", line 1010, in restore_agent
    response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,)
  File "C:\Projects\test\dialogflow\venv\lib\site-packages\google\api_core\gapic_v1\method.py", line 145, in __call__
    return wrapped_func(*args, **kwargs)
  File "C:\Projects\test\dialogflow\venv\lib\site-packages\google\api_core\grpc_helpers.py", line 69, in error_remapped_callable
    six.raise_from(exceptions.from_grpc_error(exc), exc)
  File "<string>", line 3, in raise_from
google.api_core.exceptions.InvalidArgument: 400 com.google.apps.framework.request.BadRequestException: Invalid agent zip. Missing required json file agent.json

from python-dialogflow.

galz10 avatar galz10 commented on August 14, 2024

@Archetype90 okay thank you for updating this, I'm looking into it.

from python-dialogflow.

galz10 avatar galz10 commented on August 14, 2024

@Archetype90 So i got your code to work.

Since you're not using the ExportAgentRequest, you're not providing the gcs_uri because there is no uri the result of the export is given locally. The problem here is that the response.value is a type any proto and you want a type ExportAgentResponse. You can unpack the response.value to ExportAgentResponse and this might help this issue but unpacking is a little bit difficult and confusing. So what i suggest you to do and this is what i did, was use a GCS Bucket to store the exported agent as a zip and then use that same GCS Bucket to restore the agent.


def restore_agent(agent_name,project_id):
    agent_client = AgentsClient()
    
    request = {
        "parent": "projects/"+project_id,
        "agent_uri": "gs://bucket/folder/"+agent_name+".zip"
    }

    operation= agent_client.restore_agent(request=request)
    result = operation.result(timeout=300)    
    print(f"Restore result: {result}")


def export_agent(agent_name,project_id):
    agent_client = AgentsClient()

    exported_agent_name = agent_name

    request = {
        "parent": "projects/"+project_id,
        "agent_uri": "gs://bucket/folder/"+agent_name+".zip"
    }

    operation = agent_client.export_agent(request=request)

    result = operation.result(timeout=300)    
    print(f"Export result: {result}")



def main():
    export_agent("agent-name","Project-id")
    restore_agent("agent-name","Project-id")

if __name__ == "__main__":
    main()```

If you have any questions or still have an error let me know otherwise i will close this issue. 


from python-dialogflow.

Archetype90 avatar Archetype90 commented on August 14, 2024

@galz10 Thank you for the reply and the effort.

Unfortunately I already have a copy of the code working for importing/exporting to GCS buckets. This issue is a result of me refactoring away from GCS buckets to use local storage for agents.

My end going being to export the agents locally, save it, and at a future time import that agent. I had originally assumed something was happening to the data while I was saving it, which is why I put the restore call directly into the export callback. It is disappointing to hear that the return value for the export is not supported by the restore and it is surprising to hear that it is difficult to get the formats to match.

Would you have any details for how I can convert an "any proto" to "ExportAgentRequest", as it sounds like this is my only option.

from python-dialogflow.

Archetype90 avatar Archetype90 commented on August 14, 2024

I am also not sure I fully understand the issue.

When I am inspecting response.value, it is type bytes.

According to the following documentation: https://cloud.google.com/dialogflow/es/docs/reference/rest/v2/projects.agent/restore

The restore API wants a base-64 encoded string of bytes (exactly what is returned from the export endpoint?) Could you clarify this. I am still very surprised, based on the documentation, that the output of export is not supported as input for restore.

from python-dialogflow.

raphael-silva-chatlayer avatar raphael-silva-chatlayer commented on August 14, 2024

@galz10 I was able to export with your code, but when I try to restore, I keep getting Missing required json file agent.json.

from python-dialogflow.

galz10 avatar galz10 commented on August 14, 2024

Hi @raphael-silva-chatlayer, i just ran my code again and it's working

from python-dialogflow.

raphael-silva-chatlayer avatar raphael-silva-chatlayer commented on August 14, 2024

Hey @galz10, thanks for retrying the code!
I was wondering, how are you encoding your zip file?
I'm doing encoded_file = base64.b64encode(open("agent.zip", "rb").read()).

from python-dialogflow.

galz10 avatar galz10 commented on August 14, 2024

The code above is not using the zip file, it's using the local agent_content and passes it in to restore. If you want to generate a agent.zip file you'll need to use a GCS bucket and this code


def restore_agent(agent_name,project_id):
    agent_client = AgentsClient()
    
    request = {
        "parent": "projects/"+project_id,
        "agent_uri": "gs://bucket/folder/"+agent_name+".zip"
    }

    operation= agent_client.restore_agent(request=request)
    result = operation.result(timeout=300)    
    print(f"Restore result: {result}")


def export_agent(agent_name,project_id):
    agent_client = AgentsClient()

    exported_agent_name = agent_name

    request = {
        "parent": "projects/"+project_id,
        "agent_uri": "gs://bucket/folder/"+agent_name+".zip"
    }

    operation = agent_client.export_agent(request=request)

    result = operation.result(timeout=300)    
    print(f"Export result: {result}")



def main():
    export_agent("agent-name","Project-id")
    restore_agent("agent-name","Project-id")

if __name__ == "__main__":
    main()

from python-dialogflow.

raphael-silva-chatlayer avatar raphael-silva-chatlayer commented on August 14, 2024

Got it!
I read here that I could use an encoded zip file, I guess it doesn't work then.
Thanks again!

from python-dialogflow.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.