How should I solve this problem when writing code to integrate AI?

I checked that the openai module is already installed in my local Python

Look into this: https://github.com/DynamoDS/Dynamo/wiki/Customizing-Dynamo’s-Python-3-installation

But before you go about building the tool you are looking into, confirm there is a valid use case for your LLM use. LLM tokens costs can add up quickly, and if you’re looking to automate via Dynamo then using the LLM to produce repeatable code is likely a better use of your funds.

It depends what you need to do.

The big AI providers would have you think anything AI has to be one of their LLMs- which is not necessarily the case.

As @jacob.small says- the solution might be simpler than you think. There are also a huge number of AI/machine learning models that can run locally, tailored to a specific purpose

Hi,

If they are simple requests, you can use urllib (builtin lib) with curl

example

# Load the Python Standard and DesignScript Libraries
import sys
import os 
import json
import urllib.request
import urllib.error
import json
import ssl

apikey = os.getenv("API_KEY_OPENAI")

def translate_AI(text="maison", source="french", target="english"):
    global model_input
    messages = []
    #
    prompt = "Translate the following word from {0} to {1}: {2}. Return the result in JSON format.".format(source, target, text)
    #
    messages.append({"role": "user", "content": prompt})
    # add a prompt system if necessary
    #messages.append({"role": "system", "content": self._history})
    response_data = {}
    url = "https://api.openai.com/v1/chat/completions"
    #
    payload = {
    "model": model_input, 
    "max_completion_tokens": 3000 ,
    "temperature": 1,
    "response_format": { "type": "json_object" },
    "messages": messages,
    "reasoning_effort": "medium", 
    "verbosity": "medium"
    }
    
    # Convert to JSON and encode
    json_data = json.dumps(payload).encode('utf-8')
    
    # Create request with headers
    request = urllib.request.Request(
        url,
        data=json_data,
        headers={
            "Authorization": "Bearer {}".format(apikey),
            "Content-Type": "application/json"
        },
        method='POST'
    )
    
    try:
        # Send request
        with urllib.request.urlopen(request) as response:
            status_code = response.getcode()
            response_data = json.loads(response.read().decode('utf-8'))
    #
    except urllib.error.HTTPError as e:
        print(f"Status Code: {e.code}")
        try:
            error_data = json.loads(e.read().decode('utf-8'))
            print(f"Response: {error_data}")
        except:
            print(f"Response: Error {e.code} - {e.reason}")
    #
    except Exception as e:
        print(f"Error: {e}")
        
    return response_data

model_input = "gpt-5.6-sol" # or IN[0]
# Example 
data_dict = {
"word_to_translate" : "maison",
"lang_source" : "french",
"lang_target" : "english"
}


resultB = translate_AI(data_dict["word_to_translate"], data_dict["lang_source"], data_dict["lang_target"])

result_dict = json.loads(resultB["choices"][0]["message"]["content"])


OUT = data_dict | result_dict

I used Anaconda to create the python 3.912 environment and installed the openai in this env.

Thank you very much,I created my first AI dynamo node.

Dynamo is isolated from Anaconda virtual environments. Although it is possible to link a Conda environment via sys.path, it is often simpler to install modules directly into Dynamo’s own Python environment (see the Jacob’ s link).