GithubHelp home page GithubHelp logo

xontrib-openai's Introduction

Use Open AI models in xonsh shell.

If you like the idea click ⭐ on the repo and tweet.

Installation

To install use pip:

xpip install -U xontrib-openai
# or: xpip install -U git+https://github.com/anki-code/xontrib-openai

Usage

$OPENAI_API_KEY = 'abcd1234'  # https://platform.openai.com/account/api-keys

# Defaults:
# $OPENAI_MODEL = 'text-davinci-003'  # https://platform.openai.com/docs/models/overview
# $OPENAI_MAX_TOKENS = 500

xontrib load openai

ai! hello
# Hello! How are you?

In case you need addon with new model:

aliases['ai4'] = "$OPENAI_MODEL='gpt-4' $OPENAI_MAX_TOKENS=1000 @('ai')"

ai4! hello gpt-4
# Hello human

Get shell commands

ai! how to git commit. Give me only command
# git commit -m "Commit message"
ai! how to remove all containers and images in docker. Only commands please
# docker stop $(docker ps -a -q)
# docker rm $(docker ps -a -q)
# docker rmi $(docker images -a -q)

Get Python code

ai! send post request with json data on python
# import requests
# import json
# 
# url = 'http://example.com/api/1/users'
# data = {
#   "first_name": "John",
#   "last_name": "Smith"
# }
# 
# headers = {'Content-type': 'application/json'}
# response = requests.post(url, data=json.dumps(data), headers=headers)

Generate data

ai! give me json where keys are fruits and values are most common fruit color
# {
#     "Apple": "Red",
#     "Banana": "Yellow",
#     "Orange": "Orange",
#     "Grape": "Purple",
#     "Strawberry": "Red",
#     "Lemon": "Yellow",
#     "Kiwi": "Green",
#     "Cherry": "Red",
#     "Watermelon": "Green"
# }

Using the data:

import json
j = json.loads($(ai give me small json))
j
# {'name': 'John', 'age': 25}

Known issues

To use gpt-4 model join wait list.

Credits

This package was created with xontrib template.

xontrib-openai's People

Contributors

anki-code avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

xontrib-openai's Issues

Use colors

Draft

from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import Terminal256Formatter

@aliases.register("ai")
def __ai(args):
    import openai
    openai.api_key = __xonsh__.env.get('OPENAI_API_KEY', '')
    response = openai.Completion.create(**{
        'prompt': ' '.join(args),
        'engine': __xonsh__.env.get('OPENAI_MODEL', 'text-davinci-003'),
        'max_tokens': __xonsh__.env.get('OPENAI_MAX_TOKENS', 100)
    })

    output = response.choices[0].text.strip()
    if 1:
        highlighted_code = highlight(output, PythonLexer(), Terminal256Formatter(style='monokai'))
        print(f"{highlighted_code.rstrip()}")
    
    else:
        print(output)

image

For community

⬇️ Please click the 👍 reaction instead of leaving a +1 or 👍 comment

Feature Request: Dynamic model to select

Hi! Thank you for the awesome xontrib! I gave it a star!

I've found that ... The current hard coded model (which is currently the best to select) is hard coded. I'm on the wait list for GPT four, and and have already used it through the website. When it becomes available I would have to go into the xontrib and hard coded the gpt-4 selection, and if it's limited, when I need to go back to text-dacinci or even choose gpt3.5-turbo model, I would have to go back to xontrib and adjust the code.

I'm wondering How hard would it be to write the code so that at least the three major models were part of a drop down selection?

Anyways this is all just a suggestion. The text-davinci model does everything one could currently ask.

I've already starred the repo.

Thanks

For community

⬇️ Please click the 👍 reaction instead of leaving a +1 or 👍 comment

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.