Skip to main content

A package for managing workflows with various functions.

Project description

SegmindWorkflow

SegmindWorkflow is a Python package that provides a structured approach to handling multiple API calls in a sequence. It is designed to query a prompt, manage a function call sequence based on API responses, and execute these functions in a defined order. The package keeps track of the context, cost, and credits left after each function call.

Installation

To install the SegmindWorkflow package, use pip:

pip install segmind-workflow

Usage

Import the Package

First, import the Workflow class from the segmindworkflow package:

from segmindworkflow import Workflow

Initialization

Initialize the Workflow class by passing an API key, which will be used for external API requests.

api_key = "your_api_key_here"
workflow = Workflow(api_key)

1. Query the Workflow

The query method allows you to send a prompt to the external API. Based on the prompt, the API will return a sequence of function calls and an updated context for further queries or function executions.

Parameters:

  • prompt (str): The input string (or query) you want to send to the API.
prompt = "Find the best solution for my query"
workflow.query(prompt)

This method will update:

  • The function call sequence.
  • The context for future requests.

2. Execute the Function Sequence

Once the function call sequence is generated, you can execute it using the call method. This method will go through each function in the sequence and make API requests to execute them in order. After makint the call to a function, the output of the function will be stored in the outputs folder(this is default folder and you can change it by passing output_dir param while initialising the Workflow class).

Parameters:

  • input_data (dict): The input data required by the functions in the sequence.
input_data = ["INP0", "INP1"]
workflow.call(input_data)

3. Get the Function Call Sequence

You can retrieve the sequence of function calls generated by the last query using the get_sequence method.

sequence = workflow.get_sequence()
print("Function call sequence:", sequence)

4. Get the Results

After executing the function call sequence, you can retrieve the results using the get_result method.

results = workflow.get_result()
print("Results:", results)

5. Check Remaining Credits

Each function call reduces the available credits. You can check how many credits you have left after making function calls with the total_credits_left method.

credits_left = workflow.total_credits_left()
print("Credits left:", credits_left)

6. Calculate Total Cost

The call_cost method calculates the total cost of the function call sequence based on the prices defined for each function.

total_cost = workflow.call_cost()
print("Total cost:", total_cost)

7. Reset the Workflow

If you need to reset the workflow (clear context, query sequence, etc.), you can use the reset method. This will reset everything except the credits left.

# workflow.reset()
workflow.reset(outputs = True) # also deletes the output directory

Error Handling

Each method will raise meaningful errors if they are called in the wrong order. For example:

  • Calling get_sequence() before query() will raise an error.
  • Calling get_result() before call() will raise an error.
  • You cannot calculate the cost of the function sequence before generating it with a query.

These checks ensure that the workflow is followed properly.

Example Usage

Here's a complete example that showcases the usage of the SegmindWorkflow package:

from segmindworkflow import Workflow

# Step 1: Initialize the workflow with an API key
api_key = "your_api_key"
workflow = Workflow(api_key)

# Step 2: Query the workflow with a prompt
prompt = "Optimize the cost of running my application"
workflow.query(prompt)

# Step 3: Execute the sequence of function calls
input_data = {"param1": "value1"}
workflow.call(input_data)

# Step 4: Retrieve the function call sequence
sequence = workflow.get_sequence()
print("Function Call Sequence:", sequence)

# Step 5: Retrieve the results of the function calls
results = workflow.get_result()
print("Results:", results)

# Step 6: Check remaining credits
credits_left = workflow.w_of_credits_left()
print("Credits left:", credits_left)

# Step 7: Calculate the total cost
total_cost = workflow.w_of_cost()
print("Total cost:", total_cost)

# Step 8: Reset the workflow for a new session
workflow.reset()

Code for changing the parameters of the function call sequence

from segmind_workflow import Workflow
workflow = Workflow(api_key)
prompt = 'generate an image of cat using flux dev'
workflow.query(prompt)
print("Sequence Obtained before changing anything")
print(workflow.get_sequence())
prompt = "Change the number of steps in flux dev model t0 15"
workflow.query(prompt)
print("Sequence Obtained before changing steps parameter in flux dev model")
print(workflow.get_sequence())

Code for changing the parameters of the function call sequence

from segmind_workflow import Workflow
workflow = Workflow(api_key)
prompt = 'generate an image of cat using flux dev'
workflow.query(prompt)
print("Sequence Obtained before changing anything")
print(workflow.get_sequence())
workflow.change_params({'flux_1_dev':{'steps':15}})
print("Sequence Obtained before changing steps param in flux dev model")
print(workflow.get_sequence())
workflow.change_function_names([['flux_1_dev', 'flux_1_schnell']])
print("Sequence Obtained before changing flux dev model to schnell model")
print(workflow.get_sequence())

License

This project is licensed under the MIT License.

Contributing

If you want to contribute to this package, feel free to fork the repository and submit a pull request.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

segmind_workflow-0.1.13.tar.gz (593.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

segmind_workflow-0.1.13-py3-none-any.whl (589.7 kB view details)

Uploaded Python 3

File details

Details for the file segmind_workflow-0.1.13.tar.gz.

File metadata

  • Download URL: segmind_workflow-0.1.13.tar.gz
  • Upload date:
  • Size: 593.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.12

File hashes

Hashes for segmind_workflow-0.1.13.tar.gz
Algorithm Hash digest
SHA256 6c94e2f2284f6d7b23a9b188abd7eaa079d117430478f567a46e5513f3c3091b
MD5 0d11a8d6010956681b7b6e2aa9ec1472
BLAKE2b-256 5564008ba585d56cb85dca71139c5455c134b43678b2749f7e7e382e670069a1

See more details on using hashes here.

File details

Details for the file segmind_workflow-0.1.13-py3-none-any.whl.

File metadata

File hashes

Hashes for segmind_workflow-0.1.13-py3-none-any.whl
Algorithm Hash digest
SHA256 36f3736f3c30270d9b6eedc9a35879f498c9f775da77d10a6679a40c122fc5b9
MD5 9e98acbb6e77e5279ee1058e27bbde05
BLAKE2b-256 ea914a3a5fa57230f4c92d8edb0c44ff9a8f48b9d37099da26cf7b4a508ffa68

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page