Skip to content

Commit

Permalink
fix #323
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenHodgson committed Nov 15, 2024
1 parent e991108 commit 4129b8a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
6 changes: 3 additions & 3 deletions OpenAI-DotNet/Common/Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public Function(string name, string description = null, JsonNode parameters = nu
{
if (!Regex.IsMatch(name, NameRegex))
{
throw new ArgumentException($"The name of the function does not conform to naming standards: {NameRegex}");
throw new ArgumentException($"The name of the function does not conform to naming standards: {NameRegex} \"{name}\"");
}

Name = name;
Expand Down Expand Up @@ -78,7 +78,7 @@ public Function(string name, string description, string parameters, bool? strict
{
if (!Regex.IsMatch(name, NameRegex))
{
throw new ArgumentException($"The name of the function does not conform to naming standards: {NameRegex}");
throw new ArgumentException($"The name of the function does not conform to naming standards: {NameRegex} \"{name}\"");
}

Name = name;
Expand All @@ -98,7 +98,7 @@ private Function(string name, string description, MethodInfo method, object inst
{
if (!Regex.IsMatch(name, NameRegex))
{
throw new ArgumentException($"The name of the function does not conform to naming standards: {NameRegex}");
throw new ArgumentException($"The name of the function does not conform to naming standards: {NameRegex} \"{name}\"");
}

if (functionCache.ContainsKey(name))
Expand Down
7 changes: 4 additions & 3 deletions OpenAI-DotNet/Common/Tool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -421,9 +421,10 @@ internal static bool TryGetTool(ToolCall toolCall, out Tool tool)

private static string GetFunctionName(Type type, MethodInfo methodInfo)
{
// todo possibly use string hash instead to mitigate long names?
// todo possibly use AssemblyQualifiedName?
return $"{type.FullName}.{methodInfo.Name}".Replace('.', '_');
var baseName = methodInfo.Name.Replace('.', '_');
var hashedFullyQualifiedName = $"{type.AssemblyQualifiedName}".GenerateGuid().ToString("N");
var nameLength = baseName.Length <= 32 ? baseName.Length : 32;
return $"{baseName[..nameLength]}_{hashedFullyQualifiedName}";
}

#endregion Tool Cache
Expand Down
11 changes: 11 additions & 0 deletions OpenAI-DotNet/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using System.Linq;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;

namespace OpenAI.Extensions
{
internal static class StringExtensions
{
/// <summary>
/// Generates a <see cref="Guid"/> based on the string.
/// </summary>
/// <param name="string">The string to generate the <see cref="Guid"/>.</param>
/// <returns>A new <see cref="Guid"/> that represents the string.</returns>
public static Guid GenerateGuid(this string @string)
=> new(MD5.HashData(Encoding.UTF8.GetBytes(@string)));

/// <summary>
/// Attempts to get the event data from the string data.
/// Returns false once the stream is done.
Expand Down

0 comments on commit 4129b8a

Please sign in to comment.