anki-code / anki-code/xontrib-openai

Use colors

Open
#1 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
6
Forks
2
PR merge metrics
No merged PRs in 30d

Description

Draft
```python
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**

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.