-
Oct 13th, 2024, 04:07 PM
#1
Thread Starter
Hyperactive Member
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
-
Oct 14th, 2024, 05:00 AM
#2
Hyperactive Member
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
-
Oct 14th, 2024, 05:26 AM
#3
Thread Starter
Hyperactive Member
Re: PowerShell Command Reader Not Updating??
Originally Posted by jdelano
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
-
Oct 14th, 2024, 08:11 AM
#4
New Member
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.
-
Oct 14th, 2024, 11:20 AM
#5
Thread Starter
Hyperactive Member
Re: PowerShell Command Reader Not Updating??
Originally Posted by rober1967
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|