Jump to content

How to run OpenAI Python Script in Workflow?


Recommended Posts

I'm bad at JS, so I try to use Python in workflows. Where did I go wrong?

 

Here's what I did:

  1. Right mouse button on workflow > Open in terminal
  2. Run `pip3 install -U --target=. openai` (works)
  3. Set workflow environment variable to `PYTHONPATH` to `.` 
  4. Run this script in the workflow:
    import sys
    
    query = sys.argv[1]
    
    from openai import OpenAI
    
    client = OpenAI(
        api_key="sk-proj-..."
    )
    
    response = client.chat.completions.create(
      model="gpt-4o-mini",
      messages=[
        {"role": "system", "content": "You are a copywriter who rewrites text with correct spelling and grammar, staying close to the original."},
        {"role": "user", "content": query},
      ]
    )
    
    message = response.choices[0].message.content
    
    print(message)
    
    sys.stdout.write(message)

 

A similar script in VSCode where I installed openai in a venv just works. But the one in Alfred gives me these errors in the Alfred debugger. I'm at a loss because it finds the openai module, but crashes importing a dependency.
 

[21:58:59.552] Rewrite text with AI[Hotkey] Passing output 'Can i haz all yor bases plz. They are belong to us now' to Run Script
[21:58:59.665] ERROR: Rewrite text with AI[Run Script] Traceback (most recent call last):
  File "/Users/anton/Library/Caches/com.runningwithcrayons.Alfred/Workflow Scripts/F29D32A0-1544-4C5A-BE6B-21D05122FD7B", line 5, in <module>
    from openai import OpenAI
  File "/Users/anton/Library/Application Support/Alfred/Alfred.alfredpreferences/workflows/user.workflow.3C558ED5-D9FC-4EAD-9D09-34A0468CA400/openai/__init__.py", line 8, in <module>
    from . import types
  File "/Users/anton/Library/Application Support/Alfred/Alfred.alfredpreferences/workflows/user.workflow.3C558ED5-D9FC-4EAD-9D09-34A0468CA400/openai/types/__init__.py", line 5, in <module>
    from .batch import Batch as Batch
  File "/Users/anton/Library/Application Support/Alfred/Alfred.alfredpreferences/workflows/user.workflow.3C558ED5-D9FC-4EAD-9D09-34A0468CA400/openai/types/batch.py", line 7, in <module>
    from .._models import BaseModel
  File "/Users/anton/Library/Application Support/Alfred/Alfred.alfredpreferences/workflows/user.workflow.3C558ED5-D9FC-4EAD-9D09-34A0468CA400/openai/_models.py", line 23, in <module>
    from pydantic.fields import FieldInfo
  File "/Users/anton/Library/Application Support/Alfred/Alfred.alfredpreferences/workflows/user.workflow.3C558ED5-D9FC-4EAD-9D09-34A0468CA400/pydantic/fields.py", line 17, in <module>
    from pydantic_core import PydanticUndefined
  File "/Users/anton/Library/Application Support/Alfred/Alfred.alfredpreferences/workflows/user.workflow.3C558ED5-D9FC-4EAD-9D09-34A0468CA400/pydantic_core/__init__.py", line 6, in <module>
    from ._pydantic_core import (
ModuleNotFoundError: No module named 'pydantic_core._pydantic_core'
[21:58:59.683] Rewrite text with AI[Run Script] Processing complete
[21:58:59.685] Rewrite text with AI[Run Script] Passing output '' to Copy to Clipboard

 

I would like the script to output 'Can I have all your bases, please? They are ours now.'

 

Oh, and running python3 from within the workflow directory is no problem either:
 

anton@Antons-ProMaxUltra user.workflow.3C558ED5-D9FC-4EAD-9D09-34A0468CA400 % python3
Python 3.12.5 (main, Aug  6 2024, 19:08:49) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from openai import OpenAI
>>> 

 

Edited by antonvh
omitted a log line
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...