enhance: API Key is now optional

- Update README.md by adding tips for using OpenAI in this project
This commit is contained in:
leo 2024-09-13 20:46:00 +08:00
parent 6fc972cc24
commit ab54ecee42
No known key found for this signature in database
3 changed files with 20 additions and 6 deletions

View file

@ -87,6 +87,19 @@ For **Linux** users:
* Make sure [git-credential-manager](https://github.com/git-ecosystem/git-credential-manager/releases) is installed on your linux. * Make sure [git-credential-manager](https://github.com/git-ecosystem/git-credential-manager/releases) is installed on your linux.
* Maybe you need to set environment variable `AVALONIA_SCREEN_SCALE_FACTORS`. See https://github.com/AvaloniaUI/Avalonia/wiki/Configuring-X11-per-monitor-DPI. * Maybe you need to set environment variable `AVALONIA_SCREEN_SCALE_FACTORS`. See https://github.com/AvaloniaUI/Avalonia/wiki/Configuring-X11-per-monitor-DPI.
## OpenAI
This software supports using OpenAI or other AI service that has an OpenAI comaptible HTTP API to generate commit message. You need configurate the service in `Preference` window.
For `OpenAI`:
* `Server` must be `https://api.openai.com/v1/chat/completions`
For other AI service:
* The `Server` should fill in a URL equivalent to OpenAI's `https://api.openai.com/v1/chat/completions`
* The `API Key` is optional that depends on the service
## External Tools ## External Tools
This app supports open repository in external tools listed in the table below. This app supports open repository in external tools listed in the table below.

View file

@ -95,7 +95,7 @@ namespace SourceGit.Models
public static bool IsValid public static bool IsValid
{ {
get => !string.IsNullOrEmpty(Server) && !string.IsNullOrEmpty(ApiKey) && !string.IsNullOrEmpty(Model); get => !string.IsNullOrEmpty(Server) && !string.IsNullOrEmpty(Model);
} }
public static OpenAIChatResponse Chat(string prompt, string question, CancellationToken cancellation) public static OpenAIChatResponse Chat(string prompt, string question, CancellationToken cancellation)
@ -105,6 +105,7 @@ namespace SourceGit.Models
chat.AddMessage("user", question); chat.AddMessage("user", question);
var client = new HttpClient() { Timeout = TimeSpan.FromSeconds(60) }; var client = new HttpClient() { Timeout = TimeSpan.FromSeconds(60) };
if (!string.IsNullOrEmpty(ApiKey))
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {ApiKey}"); client.DefaultRequestHeaders.Add("Authorization", $"Bearer {ApiKey}");
var req = new StringContent(JsonSerializer.Serialize(chat, JsonCodeGen.Default.OpenAIChatRequest)); var req = new StringContent(JsonSerializer.Serialize(chat, JsonCodeGen.Default.OpenAIChatRequest));

View file

@ -364,22 +364,22 @@
Text="{Binding OpenAIServer, Mode=TwoWay}"/> Text="{Binding OpenAIServer, Mode=TwoWay}"/>
<TextBlock Grid.Row="1" Grid.Column="0" <TextBlock Grid.Row="1" Grid.Column="0"
Text="{DynamicResource Text.Preference.AI.ApiKey}" Text="{DynamicResource Text.Preference.AI.Model}"
HorizontalAlignment="Right" HorizontalAlignment="Right"
Margin="0,0,16,0"/> Margin="0,0,16,0"/>
<TextBox Grid.Row="1" Grid.Column="1" <TextBox Grid.Row="1" Grid.Column="1"
Height="28" Height="28"
CornerRadius="3" CornerRadius="3"
Text="{Binding OpenAIApiKey, Mode=TwoWay}"/> Text="{Binding OpenAIModel, Mode=TwoWay}"/>
<TextBlock Grid.Row="2" Grid.Column="0" <TextBlock Grid.Row="2" Grid.Column="0"
Text="{DynamicResource Text.Preference.AI.Model}" Text="{DynamicResource Text.Preference.AI.ApiKey}"
HorizontalAlignment="Right" HorizontalAlignment="Right"
Margin="0,0,16,0"/> Margin="0,0,16,0"/>
<TextBox Grid.Row="2" Grid.Column="1" <TextBox Grid.Row="2" Grid.Column="1"
Height="28" Height="28"
CornerRadius="3" CornerRadius="3"
Text="{Binding OpenAIModel, Mode=TwoWay}"/> Text="{Binding OpenAIApiKey, Mode=TwoWay}"/>
</Grid> </Grid>
</TabItem> </TabItem>