I have followed this tutorial and it is working fine in my .Net App: /@hanxuyang0826/triggering-python-code-from-c-a-practical-guide-84b17d593dc6
However, when I replace the tutorial code in MyAppPython.py
class Calculator:
def AddInPython(self, a, b):
return a + b
with some python code from OpenAI:
from openai import OpenAI
client = OpenAI()
response = client.chatpletions.create(
model="gpt-4o-mini",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "What's in this image?"},
{
"type": "image_url",
"image_url": {
"url": ".jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg",
},
},
],
}
],
max_tokens=300,
)
print(response.choices[0])
I cant get it working.
This is my C# code after updating to try and trigger the Python file after I pasted in the OpenAI code:
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
using System.Diagnostics;
namespace MyApp.Areas.FunctionalLogic
{
public class IronPythonExample
{
public void ExecutePythonCode()
{
Debug.WriteLine("*****ExecutePythonCode*****");
// Create the IronPython scripting engine
ScriptEngine engine = Python.CreateEngine();
// Add the directory containing the Python file to the search paths
ICollection<string> searchPaths = engine.GetSearchPaths();
searchPaths.Add("../MyAppPython");
engine.SetSearchPaths(searchPaths);
Debug.WriteLine("*****searchPath Set:" + searchPaths.ToString());
// Import the Python file
dynamic visionModule = engine.ImportModule("MyAppPython");
Debug.WriteLine("*****Python file imported*****");
// Create an instance of the Vision class
dynamic vision = visionModule();
Debug.WriteLine("*****Instance Created*****");
// Call the MyAppPython method
vision();
}
}
}
It seems to be ok down as far as here, then throws an exception:
Debug.WriteLine("*****searchPath Set:" + searchPaths.ToString());
.
Exception thrown: 'IronPython.Runtime.Exceptions.ImportException' in Microsoft.Dynamic.dll
I think its something to do with my updated python file no longer being structured correctly as a Class and/or not being called correctly from my C# method, but I'm not very experienced with Python and this is my first ever attempt at using python inside a .Net app. I have been going in circles with this trying different variations. Does anyone have any suggestions?