-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrmMain.vb
More file actions
158 lines (122 loc) · 5.75 KB
/
Copy pathfrmMain.vb
File metadata and controls
158 lines (122 loc) · 5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
Public Class frmMain
Private client As OllamaClient
Private Async Sub RefreshModelsAsync()
Try
lblStatus.Text = "Loading models..."
cboModels.Items.Clear()
btnRefreshModels.Enabled = False
btnSend.Enabled = False
Dim models = Await client.ListModelsAsync()
For Each model In models
cboModels.Items.Add(model)
Next
If cboModels.Items.Count > 0 Then
cboModels.SelectedIndex = 0
btnSend.Enabled = True
lblStatus.Text = $"{models.Count} models found."
Else
lblStatus.Text = "No models found. Make sure Ollama is running with at least one model."
End If
Catch ex As Exception
lblStatus.Text = $"Error: {ex.Message}"
MessageBox.Show($"Error loading models: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
btnRefreshModels.Enabled = True
End Try
End Sub
Private Async Sub SendQuestion()
If cboModels.SelectedItem Is Nothing Then
MessageBox.Show("Please select a model first.", "No model selected", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Return
End If
If String.IsNullOrWhiteSpace(txtQuestion.Text) Then
MessageBox.Show("Please enter a question.", "No question", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Return
End If
Try
Dim modelName As String = cboModels.SelectedItem.ToString()
Dim temperature As Double = sliderTemperature.Value / 10.0
lblStatus.Text = "Sending request..."
btnSend.Enabled = False
btnRefreshModels.Enabled = False
Application.DoEvents()
Dim response = Await client.AskQuestionAsync(modelName, txtQuestion.Text, temperature)
txtResponse.Text = response
lblStatus.Text = "Response received."
Catch ex As Exception
lblStatus.Text = $"Error: {ex.Message}"
MessageBox.Show($"Error sending question: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
btnSend.Enabled = True
btnRefreshModels.Enabled = True
End Try
End Sub
Private Sub btnRefreshModels_Click(sender As Object, e As EventArgs) Handles btnRefreshModels.Click
If MsgBox("This will refresh the list of models. Any unsaved response will be lost. Continue?", MsgBoxStyle.YesNo, "Refresh Models") = MsgBoxResult.No Then
Return
Else
RefreshModelsAsync()
txtQuestion.Clear()
txtResponse.Clear()
End If
End Sub
Private Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click
SendQuestion()
btnSave.Enabled = True
End Sub
Private Sub txtQuestion_KeyDown(sender As Object, e As KeyEventArgs) Handles txtQuestion.KeyDown
' Send on Ctrl+Enter
If e.Control AndAlso e.KeyCode = Keys.Enter Then
SendQuestion()
e.SuppressKeyPress = True
End If
End Sub
Private Sub sliderTemperature_ValueChanged(sender As Object, e As EventArgs) Handles sliderTemperature.ValueChanged
lblTemperatureValue.Text = (sliderTemperature.Value / 10.0).ToString("0.0")
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Initialize client
client = New OllamaClient()
' Load models
RefreshModelsAsync()
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
If String.IsNullOrWhiteSpace(txtResponse.Text) Then
MessageBox.Show("There is no response to save.", "No Content", MessageBoxButtons.OK, MessageBoxIcon.Information)
Return
End If
Try
' Prepare a default filename with timestamp
Dim timestamp As String = DateTime.Now.ToString("yyyyMMdd_HHmmss")
SaveFileDialog1.FileName = $"Ollama_Response_{timestamp}.txt"
' Show save dialog
If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
' Get selected file path
Dim filePath As String = SaveFileDialog1.FileName
' Get model and question for metadata
Dim modelName As String = If(cboModels.SelectedItem IsNot Nothing, cboModels.SelectedItem.ToString(), "Unknown")
Dim metadata As String = $"Model: {modelName}{Environment.NewLine}" &
$"Temperature: {lblTemperatureValue.Text}{Environment.NewLine}" &
$"Question: {txtQuestion.Text}{Environment.NewLine}" &
$"Date: {DateTime.Now.ToString()}{Environment.NewLine}" &
$"----------------------------------------{Environment.NewLine}{Environment.NewLine}"
' Write to file
Using writer As New System.IO.StreamWriter(filePath)
writer.Write(metadata)
writer.Write(txtResponse.Text)
End Using
lblStatus.Text = $"Response saved to {System.IO.Path.GetFileName(filePath)}"
End If
Catch ex As Exception
MessageBox.Show($"Error saving file: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
lblStatus.Text = "Error saving file."
End Try
End Sub
Private Sub lblHelp_Click(sender As Object, e As EventArgs) Handles lblHelp.Click
If Panel1.Height > 500 Then
Panel1.Height = 51
Else
Panel1.Height = 541
End If
End Sub
End Class