Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Float Parameter Misinterpreted as Single in Function Invocation #326

Closed
StephenHodgson opened this issue Jan 22, 2025 · 1 comment · Fixed by RageAgainstThePixel/OpenAI-DotNet#404 or #327
Assignees

Comments

@StephenHodgson
Copy link
Member

StephenHodgson commented Jan 22, 2025

Discussed in #325

Originally posted by @AvatarisAI January 22, 2025

I recently started using the OpenAI Chat Completions package, and while things are going well overall, I’m encountering an issue with one of my functions.

The function has 5 parameters, one of which is a float. It seems that the framework incorrectly interprets this float value as single, causing the function invocation to fail when calling:
var funcOutput = await toolCall.InvokeFunctionAsync();

I traced the issue to the following line in Runtime/Common/Function.cs (line 477):
var methodParams = function.MethodInfo.GetParameters(); it's in Runtime/Commmon/Function.cs on line 477

Here’s what happens:

  1. The creativity parameter is passed with a value of 0.8 (as shown in requestedArgs):
    image

  2. However, after evaluating function.MethodInfo.GetParameters(), the parameter is interpreted as single:
    image

Since the parameter is defined as float in the function signature, this mismatch causes the invocation to fail.

Could you please take a look at this logic and see if there’s a fix?

Thank you!

Exception I get in function: Runtime/Common/Function.cs (line 392):

System.ArgumentException: Object of type 'System.Double' cannot be converted to type 'System.Single'.
  at System.RuntimeType.CheckValue (System.Object value, System.Reflection.Binder binder, System.Globalization.CultureInfo culture, System.Reflection.BindingFlags invokeAttr) [0x00071] in <ed969b0e627d471da4848289f9c322df>:0 
  at System.Reflection.RuntimeMethodInfo.ConvertValues (System.Reflection.Binder binder, System.Object[] args, System.Reflection.ParameterInfo[] pinfo, System.Globalization.CultureInfo culture, System.Reflection.BindingFlags invokeAttr) [0x00069] in <ed969b0e627d471da4848289f9c322df>:0 
  at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00040] in <ed969b0e627d471da4848289f9c322df>:0 
  at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in <ed969b0e627d471da4848289f9c322df>:0 
  at OpenAI.Function.InvokeInternalAsync[T] (OpenAI.Function function, System.Object[] invokeArgs) [0x00012] in .\Library\PackageCache\com.openai.unity\Runtime\Common\Function.cs:443 
  at OpenAI.Function.InvokeAsync (System.Threading.CancellationToken cancellationToken) [0x000c3] in .\Library\PackageCache\com.openai.unity\Runtime\Common\Function.cs:381 
@StephenHodgson
Copy link
Member Author

StephenHodgson commented Jan 22, 2025

potential fix for Function.cs:

diff --git forkSrcPrefix/OpenAI/Packages/com.openai.unity/Runtime/Common/Function.cs forkDstPrefix/OpenAI/Packages/com.openai.unity/Runtime/Common/Function.cs
index 4e2e34bb95155c9baacd569de511780cfbca8d0f..256eecdc16dcd31fbcd5339ba0b8ba7026e667f4 100644
--- forkSrcPrefix/OpenAI/Packages/com.openai.unity/Runtime/Common/Function.cs
+++ forkDstPrefix/OpenAI/Packages/com.openai.unity/Runtime/Common/Function.cs
@@ -502,6 +502,11 @@ namespace OpenAI
                     }
                     else
                     {
+                        if (value.GetType() != parameter.ParameterType)
+                        {
+                            value = Convert.ChangeType(value, parameter.ParameterType);
+                        }
+
                         invokeArgs[i] = value;
                     }
                 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant