jupyter-naas / drivers

Low-code Python library enabling access to APIs, tools, data sources in seconds.
GNU Affero General Public License v3.0
56 stars 11 forks source link

Create plugin driver #365

Closed jravenel closed 1 year ago

jravenel commented 1 year ago

@FlorentLvr can you specify following what we discussed today during the community call?

FlorentLvr commented 1 year ago

The goal of the driver is to be able to generate plugin with a function. It also should be able to count the number of tokens depending on the model used.

Here's how you can wrap the given Python code in a function with a docstring:

import json
import tiktoken
import naas

def num_tokens_from_string(string: str, encoding_name: str) -> int:
    """Returns the number of tokens in a text string."""
    encoding = tiktoken.get_encoding(encoding_name)
    num_tokens = len(encoding.encode(string))
    return num_tokens

def create_plugin(
    name,
    prompt,
    model="gpt-3.5-turbo-16k",
    temperature=0,
    output_path=None,    
):
   # Create output
   if not output_path:
         output_path= plugin_name.lower().replace(" ", "_") + "_plugin.json"

   # Get max tokens (dict to be stored in driver with key as model, value as int)

   # Check tokens

    # Create json
    plugin = {
        "name": name,
        "model": model,
        "temperature": temperature,
        "max_tokens": plugin_max_tokens,
        "prompt": prompt,
    }

    # Save dict to JSON file
    with open(output_path, "w") as f:
        json.dump(plugin, f)

    # Create asset
    print("💾 Plugin successfully saved. You can use it in your Naas Chat with:", asset_link)
    return asset_link
FlorentLvr commented 1 year ago

@Dr0p42, as we are developing more plugins I would like to create simple driver to start. Do you think I could implement the code above?

Can you review the parameters of the function and let me know if I miss something?