Results 1 to 5 of 5

Thread: PowerShell Command Reader Not Updating??

  1. #1

    Thread Starter
    Hyperactive Member Frabulator's Avatar
    Join Date
    Jan 2015
    Location
    USA
    Posts
    409

    PowerShell Command Reader Not Updating??

    I want to make an trainable AI inside of VB.Net environment.

    I have ollama installed on my machine and it can be accessed through CMD or PowerShell with the following

    Code:
    ollama run llama3.1
    and then you ask whatever questions you want.

    I want to have this come up in a chat style communication in a windows form.
    I have a windows form with 1 input box (Textbox1), 1 submit button (button1) and 1 chat box (RichTextbox1).

    currently my thought was the run PowerShell and read the export and put that into the chat box.

    The issue is that when I run the command to start ollama, the ollama server and application does not actually start until the windows form application is closed down. Any ideas why?


    Code:
    Imports System.Text
    Imports System.Threading.Tasks
    Imports System.IO
    
    
    Public Class Form1
    
        Private psProcess As Process
        Private writePipe As StreamWriter
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ' Initialize the PowerShell Process
            StartPowerShellProcess()
        End Sub
    
        Private Sub StartPowerShellProcess()
            psProcess = New Process()
            psProcess.StartInfo.FileName = "powershell.exe"
            psProcess.StartInfo.Arguments = "-NoExit -Command -"
            psProcess.StartInfo.RedirectStandardInput = True
            psProcess.StartInfo.RedirectStandardOutput = True
            psProcess.StartInfo.RedirectStandardError = True
            psProcess.StartInfo.UseShellExecute = False
            psProcess.StartInfo.CreateNoWindow = True
            psProcess.StartInfo.StandardOutputEncoding = Encoding.UTF8
    
            ' Start process and begin asynchronous output read
            AddHandler psProcess.OutputDataReceived, AddressOf OnOutputDataReceived
            AddHandler psProcess.ErrorDataReceived, AddressOf OnOutputDataReceived
    
            psProcess.Start()
            psProcess.BeginOutputReadLine() ' Start reading output asynchronously
            psProcess.BeginErrorReadLine()  ' Start reading error asynchronously if any
    
            ' Get the standard input pipe for sending commands
            writePipe = psProcess.StandardInput
        End Sub
    
        Private Sub OnOutputDataReceived(sender As Object, e As DataReceivedEventArgs)
            ' Handle output asynchronously
            If Not String.IsNullOrEmpty(e.Data) Then
                AppendToRichTextbox("CMD: " & e.Data)
            End If
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim input As String = TextBox1.Text
            If Not String.IsNullOrWhiteSpace(input) Then
                ' Display the user input in RichTextbox1
                AppendToRichTextbox("User: " & input)
    
                ' Send command to PowerShell and clear the textbox
                SendCommandToPowerShell(input)
                TextBox1.Clear()
            End If
        End Sub
    
        Private Sub SendCommandToPowerShell(command As String)
            ' Send command asynchronously to PowerShell
            Task.Run(Sub()
                         writePipe.WriteLine(command)
                         writePipe.Flush()
                     End Sub)
        End Sub
    
        Private Sub Textbox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
            ' Detect Enter key to trigger Button1 click
            If e.KeyCode = Keys.Enter Then
                e.SuppressKeyPress = True
                Button1.PerformClick()
            End If
        End Sub
    
        Private Sub AppendToRichTextbox(text As String)
            If RichTextBox1.InvokeRequired Then
                RichTextBox1.Invoke(New Action(Sub() AppendToRichTextbox(text)))
            Else
                RichTextBox1.AppendText(text & Environment.NewLine)
                RichTextBox1.ScrollToCaret()
            End If
        End Sub
    
        Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
            ' Close PowerShell process
            If psProcess IsNot Nothing AndAlso Not psProcess.HasExited Then
                psProcess.Kill()
                psProcess.Dispose()
            End If
        End Sub
    
    End Class
    Oops, There it goes. Yep... my brain stopped...
    _________________________________

    Useful Things:

    How to link your VB.Net application to Excel

  2. #2
    Hyperactive Member
    Join Date
    Jul 2022
    Posts
    405

    Re: PowerShell Command Reader Not Updating??

    It may be easier to use the API, here is the GitHub link https://github.com/ollama/ollama/blob/main/docs/api.md for the docs

  3. #3

    Thread Starter
    Hyperactive Member Frabulator's Avatar
    Join Date
    Jan 2015
    Location
    USA
    Posts
    409

    Re: PowerShell Command Reader Not Updating??

    Quote Originally Posted by jdelano View Post
    It may be easier to use the API, here is the GitHub link https://github.com/ollama/ollama/blob/main/docs/api.md for the docs
    I originally looked into that, but then got more confused and couldn't figure out a way to make it work in a visual basic environment.

    I realized everything could be ran through CMD, so I traveled this line of inquiry.

    ~Frab
    Oops, There it goes. Yep... my brain stopped...
    _________________________________

    Useful Things:

    How to link your VB.Net application to Excel

  4. #4
    New Member
    Join Date
    Mar 2022
    Posts
    1

    Re: PowerShell Command Reader Not Updating??

    Try removing that option to let the ollama command run without blocking your form. Also, make sure the command works in a standalone PowerShell window first. If it runs fine there, you could be facing a permissions issue or need to run it in a separate thread to avoid UI blocking.

  5. #5

    Thread Starter
    Hyperactive Member Frabulator's Avatar
    Join Date
    Jan 2015
    Location
    USA
    Posts
    409

    Re: PowerShell Command Reader Not Updating??

    Quote Originally Posted by rober1967 View Post
    Try removing that option to let the ollama command run without blocking your form. Also, make sure the command works in a standalone PowerShell window first. If it runs fine there, you could be facing a permissions issue or need to run it in a separate thread to avoid UI blocking.
    What option are you talking about removing?

    And yes, the command works fine in normal power shell.

    I tried running the application from the bin as administrator, but got the same result. The Ollama Service did not run until after the form closed.

    I can still run other CMD commands, like ipconfig. As soon as I run ollama, it’s like the command window stalls and only runs after the program closes.
    Oops, There it goes. Yep... my brain stopped...
    _________________________________

    Useful Things:

    How to link your VB.Net application to Excel

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width