Skip to main content

Transform document into function

Project description

com2fun - Transform document into function.

This liabrary leverages OpenAI API to predict the output of a function based on its documentation.

Install

pip install --upgrade com2fun

Usage

Basic usage:

import com2fun

@com2fun.com2fun
def top(category: str, n) -> list[str]:
    """top n items"""

In  [1]: top("fish", 5)
Out [1]: ['salmon', 'tuna', 'cod', 'mackerel', 'halibut']
In  [2]: type(top("fish", 5))
Out [2]: list
In  [3]: top("Pen Brand", 3)
Out [3]: ['Pilot', 'Uni-ball', 'Zebra']

Specifiy output format by document:

@com2fun.com2fun
def SWOT(action: str) -> dict:
    """
    SWOT analysis is a powerful tool used to assess an organization’s strengths, 
    weaknesses, opportunities, and threats. It helps organizations focus their 
    resources and actions on areas where they have the most potential for success 
    and makes strategic decisions more transparent.
    
    Generate a SWOT analysis to assist business owners, managers, and individuals 
    in making tough decisions.
    
    Return a dictionary like 
    {
        "strengths": [ (summary, explanation), ...],
        "weaknesses": [...],
        "oppotunities": [...],
        "threats": [...],
    }
    """

In  [4]: print(SWOT("provide additional training for customer service staff"))
Out [4]:
{'strengths': [('Increased customer satisfaction',
   'Providing additional training for customer service staff will help them better understand customer needs and provide better service, leading to increased customer satisfaction.')],
 'weaknesses': [('Cost of training',
   'Providing additional training for customer service staff will require additional resources, such as time and money, which can be a significant cost to the organization.')],
 'opportunities': [('Improved customer service',
   'Providing additional training for customer service staff will help them better understand customer needs and provide better service, leading to improved customer service.')],
 'threats': [('Lack of resources',
   'Providing additional training for customer service staff may require additional resources, such as time and money, which may not be available to the organization.')]}

Chain of thought:

@com2fun.com2fun
def solve_elementary_math(question: str) -> dict:
    """
    {"debug": {"step by step explanation": list[str]},
     "return": float}
    """
In  [5]: solve_elementary_math("Maurita and Felice each take 4 tests. Here are the results of Maurita’s 4 tests: 4, 4, 4, 4. Here are the results for 3 of Felice’s 4 tests: 3, 3, 3. If Maurita’s mean for the 4 tests is 1 point higher than Felice’s, what’s the score of Felice’s 4th test?")
Out [5]:
{'debug': {'step by step explanation': ['Maurita and Felice each took 4 tests.',
   'Maurita got 4, 4, 4, 4 on her tests.',
   'Felice got 3, 3, 3 on 3 of her tests.',
   'Maurita’s mean for the 4 tests is 1 point higher than Felice’s.',
   'We need to find the score of Felice’s 4th test.',
   'Maurita’s mean is 4, so Felice’s mean must be 3.',
   'The sum of Felice’s 4 tests must be 3 + 3 + 3 + x = 12.',
   'Therefore, x = 12 - 9 = 3.',
   'The score of Felice’s 4th test is 3.']},
 'return': 3.0}

Add Example

In  [1]: top.add_example('continents', 3)(['Asia', 'Africa', 'North America'])

Set Client

By default, com2fun uses the OpenAI library openai globally. You can set the client for com2fun by

@com2fun.com2fun(client=openai.OpenAI(api_key="your_api_key"))

Anthropic is also supported:

@com2fun.com2fun(
    SF=com2fun.SF.anthropic.AnthropicChatSF, client=anthropic.Anthropic()
)

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

com2fun-0.1.10.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

com2fun-0.1.10-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

Details for the file com2fun-0.1.10.tar.gz.

File metadata

  • Download URL: com2fun-0.1.10.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.3 Linux/5.10.192-1-MANJARO

File hashes

Hashes for com2fun-0.1.10.tar.gz
Algorithm Hash digest
SHA256 1d5af4edf009b752ab53c2791db85e32958944148bd0c58fed6c91a940e5ad68
MD5 29ddbe8215b5aeb4b9a27da11763876f
BLAKE2b-256 4c405dd39e546cb3354e2bdf54f9a1523ff9d04306cc808a98ddecb12a71a7dd

See more details on using hashes here.

File details

Details for the file com2fun-0.1.10-py3-none-any.whl.

File metadata

  • Download URL: com2fun-0.1.10-py3-none-any.whl
  • Upload date:
  • Size: 12.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.3 Linux/5.10.192-1-MANJARO

File hashes

Hashes for com2fun-0.1.10-py3-none-any.whl
Algorithm Hash digest
SHA256 3c7b51a9c752416d570fefd7550c760fd586d03fedf3510d134e6c4672ff059c
MD5 d1dba45d290fcf889e0667e0d468d410
BLAKE2b-256 60df2a9f939d840f2735395b60dcc3d06a5526e077512ae720405b76d524f3c4

See more details on using hashes here.

Supported by

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