OkGoDoIt / OpenAI-API-dotnet

An unofficial C#/.NET SDK for accessing the OpenAI GPT-3 API

Home Page:https://www.nuget.org/packages/OpenAI/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Typically each socket address (protocol/network address/port) is only allowed to be used once. (127.0.0.1:9127)

liujianjie opened this issue · comments

Hello, please allow me to introduce my problem. I hope someone can see it.

  • Problem Description

    My requirement is for OpenAI to help translate Chinese into the corresponding language. Since there are multiple Chinese languages, each to be translated is a Task. I use List to add all Tasks, but I have SemaphoreSlim to control the number of concurrencies at a time. , I currently set it to 50. But after the network request, it was informed that Typically each socket address (protocol/network address/port) is only allowed to be used once. (127.0.0.1:9127)

  • code

    private async Task StratTranslate()
    {
        List<Task> tasks = new List<Task>();
        var semaphore = new SemaphoreSlim(int.Parse(Data.MaxTaskCount));// 50
        foreach (var diffExcel in diffExcelHandlerList)// need to translate
        {
            var languageStr = LanguageHelp.GetLangugeStrByEnum(diffExcel.languageType);
            foreach (var cellCls in diffExcel.translateCellCls_List)
            {
                tasks.Add(TranslateTextAsyncWithCell(cellCls, languageStr, semaphore));
            }
        }
        await Task.WhenAll(tasks);
    }
    private async Task TranslateTextAsyncWithCell(TranslateExcelCellCls cellCls, string translateLanguageType, SemaphoreSlim semaphore)
    {
        try
        {
            await semaphore.WaitAsync();
            string translatedText = await openAIAPINetReq.TranslateTextNewChatAsync(cellCls.chineseText, translateLanguageType);
            cellCls.openAI_translateStr = translatedText;
            //await Task.Delay(2000);
        }
        catch (Exception e)
        {
            string errorStr = $"Error {cellCls.chineseText} for cell at row {cellCls.row} and column {cellCls.chinese_Column}: {e.Message}";
            Log.Error(errorStr);
            cellCls.openAI_translateStr = string.Empty;
        }
        finally
        {
            semaphore.Release();
        }
    }
    public async Task<string> TranslateTextNewChatAsync(string chineseStr, string translateType)
    {
        try
        {
            var result = await api.Chat.CreateChatCompletionAsync(new ChatRequest()
            {
                Model = Model.GPT4_Turbo,
                Temperature = 0.7,
                //MaxTokens = 50,
                Messages = new ChatMessage[] {
                    new ChatMessage(ChatMessageRole.System, Configure.GetSystemPrompTranslateTextStr(translateType)),
                    new ChatMessage(ChatMessageRole.User, chineseStr)
                }
            });
    
            var reply = result.Choices[0].Message;
            var response = reply.Content.Trim();
            Log.Info($"{chineseStr} : {response}");
            return response;
        }
        catch (Exception e)
        {
            Log.Error($"Error translating {chineseStr}: {e.Message}");
            return "";
        }
    }
  • Error

    [17:31:20] Error translating 2{0}: Typically each socket address (protocol/network address/port) is only allowed to be used once (api.openai.com:443)
    [17:31:20] Error translating 3月{0}: Typically each socket address (protocol/network address/port) is only allowed to be used once (api.openai.com:443)
    [17:31:20] Error translating 4月{0}: Typically each socket address (protocol/network address/port) is only allowed to be used once (api.openai.com:443)