PawanOsman / ChatGPT.Net

C# library for ChatGPT using official OpenAI API
https://www.nuget.org/packages/ChatGPT.Net
MIT License
379 stars 71 forks source link

Hangs at await chatGpt.WaitForReady(); forever #1

Closed jgric2 closed 1 year ago

jgric2 commented 1 year ago

Hi there, I cant seem to get the api to get past await chatGpt.WaitForReady();

it just hangs forever, any ideas?

Thanks James

PawanOsman commented 1 year ago

Hi, thank you for opening this issue,

can you provide more information? What is the type of authentication you are using Microsoft account or Session Token? you run it on desktop or server? Windows or linux? Do you use xvfb to run it?

jgric2 commented 1 year ago

Hi thanks for the fast response. I used the session token Run on my desktop Windows 10 No idea what xvfb is sorry. Thank you :)

PawanOsman commented 1 year ago

Thank you for your information.

xvfb is a Linux package needed when you run your application on a server without an actual display screen, it will create a virtual one,

while you are running it on your Desktop then it is not needed,

do you see the Chromium browser open?

jgric2 commented 1 year ago

I see. Well I already had chrome open so maybe that was the problem. But I did not see a new chromium window open. Just out getting lunch I'll drive home soon and debug it again :)

jgric2 commented 1 year ago

Thank you for your information.

xvfb is a Linux package needed when you run your application on a server without an actual display screen, it will create a virtual one,

while you are running it on your Desktop then it is not needed,

do you see the Chromium browser open?

the chromium browser does not open.

Could it be the latitude and longitude hardcoded into Init()?

PawanOsman commented 1 year ago

Thank you, I found the problem, you need to install chromium and other headless browser tools before running it because it uses playwright to control the headless browser and its Chromium and other WebKit tools must be installed, I installed it before that's why I forgot to put it in the readme file

to fix this you need to use PowerShell 7+ if you don't have you can download it here

then after building your project open PowerShell 7 in your project directory and run the following command

pwsh bin/Debug/netX/playwright.ps1 install

netX is your .Net version (eg. net6.0)

and wait it will install all needed browsers and tools for the playwright browser

then you can run your project again it must be working fine 😁

** thank you for informing me of this issue, I will add these steps to the readme file.

jgric2 commented 1 year ago

Thank you, I found the problem, you need to install chromium and other headless browser tools before running it because it uses playwright to control the headless browser and its Chromium and other WebKit tools must be installed, I installed it before that's why I forgot to put it in the readme file

to fix this you need to use PowerShell 7+ if you don't have you can download it here

then after building your project open PowerShell 7 in your project directory and run the following command

pwsh bin/Debug/netX/playwright.ps1 install

netX is your .Net version (eg. net6.0)

and wait it will install all needed browsers and tools for the playwright browser

then you can run your project again it must be working fine 😁

** thank you for informing me of this issue, I will add these steps to the readme file.

Yep that fixes it! thank you!

jgric2 commented 1 year ago

Is it possible to set the browser to headless?

PawanOsman commented 1 year ago

unfortunately, it will not work because it can't bypass Cloudflare protection with headless, if you are planning to run it on a Linux server you can use xvfb, or just minimize the browser window in Desktop,

I will try to find a solution in the next updates...

jgric2 commented 1 year ago

unfortunately, it will not work because it can't bypass Cloudflare protection with headless, if you are planning to run it on a Linux server you can use xvfb, or just minimize the browser window in Desktop,

I will try to find a solution in the next updates...

I found a temporary solution! perhaps you can streamline it as I am not to familiar with microsoft playwright.

the solution I found was to get the current running process of all chrome windows (as I dont know how to get the Process ID of the Chromium browsers generated by microsoft playwright and they all have the same name "Chrome")

so before playwright spawns any chrome window get the current users chrome windows that are open using Process[] p = Process.GetProcessesByName("chrome");

from there once the playwright has opened the chrome windows run this function

 [DllImport("user32.dll")]
    private static extern bool ShowWindow(IntPtr hWnd, WindowShowStyle nCmdShow);

    /// <summary>Enumeration of the different ways of showing a window using 
    /// ShowWindow</summary>
    private enum WindowShowStyle : uint
    {
        Hide = 0,
        ShowNormal = 1,
        ShowMinimized = 2,
        ShowMaximized = 3,
        Maximize = 3,
        ShowNormalNoActivate = 4,
        Show = 5,
        Minimize = 6,
        ShowMinNoActivate = 7,
        ShowNoActivate = 8,
        Restore = 9,
        ShowDefault = 10,
        ForceMinimized = 11
    }
public void HideChromium(Process[] pOriginal)
    {
        Process[] p2 = Process.GetProcessesByName("chrome");    
        for (int i = 0; i < p2.Length; i++)
        {
            var curr = p2[i];
            var found = pOriginal.Where(x => x.Id == curr.Id).ToList();
            if (found.Count() == 0)
            {
                IntPtr hWnd = curr.MainWindowHandle;
                ShowWindow(hWnd, WindowShowStyle.Hide);
            }
        }
    }

^ the hide code above will make it disappear from the taskbar :)

you can also set the browser size to (1,1) and use the header "--window-position=-4096,-4096", this will set the browser to be super small and then move it off screen, add that with hiding the window with the code above and it does a pretty good job at hiding the chromium browser.

Hope this helps :) Perhaps you can come up with a more efficent method to getting the process ID's from playwrite :)

PawanOsman commented 1 year ago

Thank you for sharing your solution. I appreciate the effort you put into finding a solution to this issue.

I will definitely consider implementing your solution in the project as an option to hide the browser. However, I also want to try using the "visible" parameter in Playwright to see if that can hide the browser as well. If the "visible" parameter doesn't work, then I will consider implementing your solution as an alternative.

Again, thank you for your help and for taking the time to share your solution with me. Your assistance is greatly appreciated.

jgric2 commented 1 year ago

Thank you for sharing your solution. I appreciate the effort you put into finding a solution to this issue.

I will definitely consider implementing your solution in the project as an option to hide the browser. However, I also want to try using the "visible" parameter in Playwright to see if that can hide the browser as well. If the "visible" parameter doesn't work, then I will consider implementing your solution as an alternative.

Again, thank you for your help and for taking the time to share your solution with me. Your assistance is greatly appreciated.

no problem :) I will continue to debug throughout the night and get back to you with any findings, Ill keep a close eye on any updates you commit :)

PawanOsman commented 1 year ago

I added the "Invisible" parameter, now you can set it to true it will start the chrome window out of the screen, about hiding it from the taskbar, thank you for the code you shared with me, but it will limit the project to be used in windows only, the project is cross-platform and I need to find a way that supported all platforms

thank you again, if I found any way to hide it from the taskbar too without limiting the project to windows only I will add it 😁

jgric2 commented 1 year ago

I added the "Invisible" parameter, now you can set it to true it will start the chrome window out of the screen, about hiding it from the taskbar, thank you for the code you shared with me, but it will limit the project to be used in windows only, the project is cross-platform and I need to find a way that supported all platforms

thank you again, if I found any way to hide it from the taskbar too without limiting the project to windows only I will add it 😁

No problems. I will add it to mine though as mine will only be windows based :). Thanks for your hard work!