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

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

Open
liujianjie opened this issue May 10, 2024 · 0 comments

Comments

@liujianjie
Copy link

liujianjie commented May 10, 2024

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant