chainfury.client

class chainfury.client.Subway(_url, _session, _trailing='')[source]

Bases: object

Simple code that allows writing APIs by .attr.ing them. This is inspired from gRPC style functional calls which hides the complexity of underlying networking. This is useful when you are trying to debug live server directly.

If you want to setup a client, use the ``get_client`` function, this is not what you are looking for.

Note

User is solely responsible for checking if the certain API endpoint exists or not. This simply wraps the API calls and does not do any validation.

Example

>>> from chainfury.client import Subway
>>> from requests import Session
>>> session = Session()
>>> session.headers.update({"token": token})
>>> stub = Subway("http://localhost:8000", session)
>>> get_chain = stub.chatbot.u("6ln9ksln")       # http://localhost:8000/chatbot/6ln9ksln
>>> chain = get_chain()                          # call like a function
{
    'name': 'funny-bot-1',
    'description': None,
    'dag': {
        'nodes': [
            {
                'id': 'bc1bdc37-07d9-49b4-9e09-b0e58a535da5_934.2328674347034',
                'cf_id': 'bc1bdc37-07d9-49b4-9e09-b0e58a535da5',
                'position': {'x': -271.25233176301117, 'y': 78.20693852768798},
                'type': 'FuryEngineNode',
                'width': 350,
                'height': 553,
                'selected': True,
                'position_absolute': None,
                'dragging': False,
                'data': {}
            }
        ],
        'edges': [],
        'sample': {
            'bc1bdc37-07d9-49b4-9e09-b0e58a535da5_934.2328674347034/model': 'gpt-3.5-turbo'
        },
        'main_in': 'bc1bdc37-07d9-49b4-9e09-b0e58a535da5_934.2328674347034/animal',
        'main_out': 'bc1bdc37-07d9-49b4-9e09-b0e58a535da5_934.2328674347034/text'
    },
    'engine': 'fury',
    'deleted_at': None,
    'created_by': 'cihua4hh',
    'id': '6ln9ksln',
    'meta': None,
    'created_at': '2023-06-27T18:05:17.395260'
}
Parameters:
  • _url (str) – The url to use for the client

  • _session (requests.Session) – The session to use for the client

__call__(method='get', trailing='', json={}, data=None, params: Dict = {}, _verbose=False, **kwargs) Tuple[Dict[str, Any], bool][source]

Call the API endpoint as if it is a function.

Parameters:
  • method (str, optional) – The method to use. Defaults to “get”.

  • trailing (str, optional) – The trailing url to use. Defaults to “”.

  • json (Dict[str, Any], optional) – The json to use. Defaults to {}.

  • data ([type], optional) – The data to use. Defaults to None.

  • params (Dict, optional) – The params to use. Defaults to {}.

  • _verbose (bool, optional) – Whether to print the response or not. Defaults to False.

Returns:

The response and whether there was an error or not

Return type:

Tuple[Dict[str, Any], bool]

u(attr: str) Subway[source]

In cases where the api might start with a number you cannot write in python, this method can be used to access the attribute.

Example

>>> stub.9jisjfi      # python will cry, invalid syntax: cannot start with a number
>>> stub.u('9jisjfi') # do this instead
Parameters:

attr (str) – The attribute to access

Returns:

The new subway object

Return type:

Subway

chainfury.client.get_client(prefix: str = '/api/', url='', token: str = '', trailing: str = '/') Subway[source]

This function returns a Subway object that can be used to interact with the API.

Example

>>> from chainfury import get_client
>>> client = get_client()
>>> chains = client.api.chains() # GET /api/chains
>>> chains

Note

The get_client function is a convenience function that can be used to get a client object. It is not required to use the library. Under the hood, it still will call the chainfury REST endpoints.

Parameters:
  • prefix (str, optional) – The prefix to use for the client. Defaults to “api/v1”.

  • url (str, optional) – The url to use for the client or picks from CF_URL env var. Defaults to “”.

  • token (str, optional) – The token to use for the client or picks from CF_TOKEN env var. Defaults to “”.

Raises:

ValueError – If no url or token is provided.

Returns:

A Subway object that can be used to interact with the API.

Return type:

Subway