Skip to content

Commit

Permalink
Add openai tools
Browse files Browse the repository at this point in the history
  • Loading branch information
ejohb committed Oct 17, 2024
1 parent 493135b commit 1e1bfdc
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
5 changes: 5 additions & 0 deletions fmtr/tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@
except ImportError as exception:
interface = MissingExtraMockModule('interface', exception)

try:
from fmtr.tools import openai_tools as openai
except ImportError as exception:
openai = MissingExtraMockModule('openai', exception)


__all__ = [
'config',
Expand Down
40 changes: 40 additions & 0 deletions fmtr/tools/openai_tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from functools import lru_cache
from openai import OpenAI

from fmtr.tools import environment_tools

OPENAI_TOKEN_KEY = 'FMTR_OPENAI_API_KEY'


@lru_cache
def get_client():
"""
Get an OpenAI client instance
"""
OPENAI_KEY = environment_tools.get(OPENAI_TOKEN_KEY)
client = OpenAI(api_key=OPENAI_KEY)
return client


def get_text(prompt, model='gpt-4o'):
"""
Very simple prompt-to-output
"""
client = get_client()
messages = [{"role": 'user', "content": prompt}]

completion = client.chat.completions.create(
messages=messages,
model=model
)
text = completion.choices[0].message.content
return text


if __name__ == '__main__':
text = get_text('hi')
text
2 changes: 1 addition & 1 deletion fmtr/tools/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.9.7
0.9.8
1 change: 1 addition & 0 deletions requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
'metric': ['pandas'],
'html': ['html2text'],
'interface': ['streamlit'],
'openai.api': ['openai']
}

CONSOLE_SCRIPTS = [
Expand Down

0 comments on commit 1e1bfdc

Please sign in to comment.