Skip to content

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

Description

@liujianjie

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)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions