Skip to content

Commit

Permalink
Merge branch 'feature_openai对外接口'
Browse files Browse the repository at this point in the history
  • Loading branch information
xuzeyu91 committed Feb 18, 2024
2 parents 3d5be56 + 50415b1 commit 34c06b1
Show file tree
Hide file tree
Showing 10 changed files with 567 additions and 72 deletions.
25 changes: 25 additions & 0 deletions AntSK/AntSK.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions AntSK/Layouts/OpenLayout.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@namespace AntSK
@inherits LayoutComponentBase

@Body

@code {

}
34 changes: 24 additions & 10 deletions AntSK/Pages/AppPage/AppOpen.razor
Original file line number Diff line number Diff line change
@@ -1,41 +1,55 @@
@namespace AntSK.Pages.AppPage
@page "/App/Open/{AppID}"


<AntDesign.Col Lg="24" Md="24">
<AntDesign.Col Lg="24" Md="24">
<Card Class="tabsCard">
<CardTabs>
<Tabs DefaultActiveKey="1">
<TabPane Key="1">
<TabTemplate>Api访问</TabTemplate>
<TabTemplate>Api接入</TabTemplate>
<ChildContent>
<Card>
<Form Model="@_appModel"
Style="margin-top: 8px;"
>
Style="margin-top: 8px;">
<FormItem Label="接口地址" LabelCol="_formItemLayout.LabelCol" WrapperCol="_formItemLayout.WrapperCol">
<Input @bind-Value="@_openApiUrl" ReadOnly="true" />

</FormItem>
<FormItem Label="秘钥" LabelCol="_formItemLayout.LabelCol" WrapperCol="_formItemLayout.WrapperCol">
<Input @bind-Value="@context.SecretKey" ReadOnly="true" />
<Button OnClick="Reset" Style="margin-top:5px;">
重置
</Button>
</FormItem>
<FormItem Label="使用方法" LabelCol="_formItemLayout.LabelCol" WrapperCol="_formItemLayout.WrapperCol">
<FormItem Label="使用方法" LabelCol="_formItemLayout.LabelCol" WrapperCol="_formItemLayout.WrapperCol">
<TextArea @bind-Value="@_desc" ReadOnly="true" Rows="15" />
</FormItem>
</Form>
</Card>
</ChildContent>
</TabPane>
<TabPane Key="2">
<TabTemplate>免登录窗口</TabTemplate>
<TabTemplate>页面嵌入</TabTemplate>
<ChildContent>

<Form Model="@_appModel"
Style="margin-top: 8px;">
<FormItem Label="页面地址" LabelCol="_formItemLayout.LabelCol" WrapperCol="_formItemLayout.WrapperCol">
<Input @bind-Value="@_openChatUrl" ReadOnly="true" />
<a href="@_openChatUrl" target="_blank">打开</a>
</FormItem>
<FormItem Label="嵌入脚本" LabelCol="_formItemLayout.LabelCol" WrapperCol="_formItemLayout.WrapperCol">
<TextArea @bind-Value="@_script" ReadOnly="true" Rows="5" />
<span>
将上述代码添加到站点的 body 中<br/>
data-width:宽度,默认 30rem<br />
data-height:高度,默认 50rem<br />
data-message-icon-url:嵌入消息图标地址,默认不设置<br />
data-color:消息图标颜色,默认 #4e83fd<br />
</span>
</FormItem>
</Form>
</ChildContent>
</TabPane>
</TabPane>
</Tabs>
</CardTabs>
</Card>
Expand Down
20 changes: 16 additions & 4 deletions AntSK/Pages/AppPage/AppOpen.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,39 @@ public partial class AppOpen

private Apps _appModel = new Apps();

private string _openApiUrl { get; set; }
private string _openApiUrl { get; set; }

private string _openChatUrl { get; set; }

private string _desc { get; set; }

private string _script { get; set; }

protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
_appModel = _apps_Repositories.GetFirst(p => p.Id == AppId);
_openApiUrl = NavigationManager.BaseUri + "api/v1/chat/completions";
_openChatUrl= NavigationManager.BaseUri + "openchat/"+AppId;
GetDesc();
GetScript();
}

private void GetDesc()
{
_desc = @$"为了方便其他应用对接,接口符合openai规范,省略了温度TopP等参数。{Environment.NewLine}BaseUrl:{Environment.NewLine}{_openApiUrl} {Environment.NewLine}headers:{Environment.NewLine}Authorization: ""{_appModel.SecretKey}"" {Environment.NewLine}Body: {Environment.NewLine}{JsonConvert.SerializeObject(new OpenAIModel() { messages=new List<OpenAIMessage>() { new OpenAIMessage() { role="user",content="你好,你是谁"} } }, Formatting.Indented)}";
_desc = @$"为了方便其他应用对接,接口符合openai规范,省略了温度TopP等参数。{Environment.NewLine}BaseUrl:{Environment.NewLine}{_openApiUrl} {Environment.NewLine}headers:{Environment.NewLine}Authorization: ""{_appModel.SecretKey}"" {Environment.NewLine}Body: {Environment.NewLine}{JsonConvert.SerializeObject(new OpenAIModel() { messages = new List<OpenAIMessage>() { new OpenAIMessage() { role = "user", content = "你好,你是谁" } } }, Formatting.Indented)}";
}

private void GetScript()
{
_script = $"<script src=\"{NavigationManager.BaseUri}js/iframe.js\" data-width=\"40rem\" data-height=\"80vh\" id=\"antsk-iframe\" data-src=\"{NavigationManager.BaseUri}openchat/{AppId}\" data-color=\"#4e83fd\" data-message-icon-url=\"{NavigationManager.BaseUri}assets/ai.png\"></script>";
}

private void HandleSubmit()
{
}

private async Task Reset()
private async Task Reset()
{
var content = "是否确认重置秘钥,重之后之前的秘钥将无法访问API接口";
var title = "重置";
Expand All @@ -72,7 +83,8 @@ private async Task Reset()
_apps_Repositories.Update(_appModel);
GetDesc();
}

}

}
}
116 changes: 58 additions & 58 deletions AntSK/Pages/ChatPage/Chat.razor
Original file line number Diff line number Diff line change
Expand Up @@ -7,80 +7,80 @@
@using AntSK.Services.Auth
@inherits AuthComponentBase

<GridRow Gutter="(16, 16)">
<GridCol Span="12">
<Spin Size="large" Tip="请稍等..." Spinning="@(_loading)">
<Card Style="height:800px;overflow: auto;">
<TitleTemplate>
<Icon Type="setting" /> 选择应用
<GridRow Gutter="(16, 16)">
<GridCol Span="12">
<Spin Size="large" Tip="请稍等..." Spinning="@(_loading)">
<Card Style="height:800px;overflow: auto;">
<TitleTemplate>
<Icon Type="setting" /> 选择应用
<Select DataSource="@_list"
@bind-Value="@AppId"
DefaultValue="@("lucy")"
ValueProperty="c=>c.Id"
LabelProperty="c=>c.Name"
Style="width:200px">
</Select>
</TitleTemplate>
<Body>
<div id="scrollDiv" style="height: 530px; overflow-y: auto; overflow-x: hidden;">
<GridRow Gutter="(8, 8)">
<Virtualize Items="@(MessageList.OrderByDescending(o => o.CreateTime).ToList())" Context="item">
<GridCol Span="24">
<Card Size="small">
<TitleTemplate>
<Text Strong><Icon Type="bulb" /> @(item.Questions)</Text>
</TitleTemplate>
<Extra>
<Space>
<SpaceItem>
<a style="color: gray;" @onclick="@(() => OnCopyAsync(item))"><Icon Type="copy" /></a>
</SpaceItem>
<SpaceItem>
<a style="color: gray;" @onclick="@(() => OnClearAsync(item.ID))"><Icon Type="rest" /></a>
</SpaceItem>
</Space>
</Extra>
<Body>
@((MarkupString)(item.HtmlAnswers))
</Body>
</Card>
</GridCol>
</Virtualize>
</GridRow>
</div>
<div style="height: 10px;"></div>
<AntDesign.Input @bind-Value="@(_messageInput)" DebounceMilliseconds="@(-1)" Placeholder="输入消息回车发送" OnPressEnter="@(async () => await OnSendAsync())" Disabled="@Sendding">
<Suffix>
<Button Icon="send" Type="@(ButtonType.Link)" OnClick="@(async () => await OnSendAsync())" Disabled="@Sendding"></Button>
</Suffix>
</AntDesign.Input>
</Body>
</Card>
</Spin>
</GridCol>
<GridCol Span="12">
<Card Style="height: 800px;overflow: auto;">
<TitleTemplate>
<Icon Type="search" /> 调试结果
</TitleTemplate>
<Extra>

</Extra>
<Body>
<div id="scrollDiv" style="height: 530px; overflow-y: auto; overflow-x: hidden;">
<GridRow Gutter="(8, 8)">
<Virtualize Items="@(MessageList.OrderByDescending(o => o.CreateTime).ToList())" Context="item">
<GridCol Span="24">
<Card Size="small">
<TitleTemplate>
<Text Strong><Icon Type="bulb" /> @(item.Questions)</Text>
</TitleTemplate>
<Extra>
<Space>
<SpaceItem>
<a style="color: gray;" @onclick="@(() => OnCopyAsync(item))"><Icon Type="copy" /></a>
</SpaceItem>
<SpaceItem>
<a style="color: gray;" @onclick="@(() => OnClearAsync(item.ID))"><Icon Type="rest" /></a>
</SpaceItem>
</Space>
</Extra>
<Body>
@((MarkupString)(item.HtmlAnswers))
</Body>
</Card>
</GridCol>
</Virtualize>
</GridRow>
</div>
<div style="height: 10px;"></div>
<AntDesign.Input @bind-Value="@(_messageInput)" DebounceMilliseconds="@(-1)" Placeholder="输入消息回车发送" OnPressEnter="@(async () => await OnSendAsync())" Disabled="@Sendding">
<Suffix>
<Button Icon="send" Type="@(ButtonType.Link)" OnClick="@(async () => await OnSendAsync())" Disabled="@Sendding"></Button>
</Suffix>
</AntDesign.Input>
</Body>
</Card>
</Spin>
</GridCol>
<GridCol Span="12">
<Card Style="height: 800px;overflow: auto;">
<TitleTemplate>
<Icon Type="search" /> 调试结果
</TitleTemplate>
<Extra>

</Extra>
<Body>
<AntList Bordered DataSource="@RelevantSources">
<ChildContent Context="item">
<span> <b>@item.SourceName </b> 相似度:<Text Mark> @item.Relevance</Text></span>
<Body>
@((MarkupString)(@item.Text))
</Body>
</ChildContent>
</AntList>
<span> <b>@item.SourceName </b> 相似度:<Text Mark> @item.Relevance</Text></span>
<Body>
@((MarkupString)(@item.Text))
</Body>
</ChildContent>
</AntList>
</Body>
</Card>
</GridCol>
</GridRow>


@code {
@code {

}
45 changes: 45 additions & 0 deletions AntSK/Pages/ChatPage/OpenChat.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
@namespace AntSK.Pages.ChatPage
@using AntSK.Domain.Repositories
@using AntSK.Models
@using Microsoft.AspNetCore.Components.Web.Virtualization
@page "/OpenChat/{AppId}"
@layout OpenLayout

<div id="chat" style="display:flex; flex-direction:column; height:100%; overflow-x:hidden;">
<PageHeader Class="site-page-header" Title="@app.Name" Subtitle="@app.Describe" />
<div id="scrollDiv" style="flex:1; width:100%; overflow-y:auto; overflow-x:hidden;padding:10px;">
<Virtualize Items="@(MessageList.OrderByDescending(o => o.CreateTime).ToList())" Context="item">
<GridCol Span="24">
<Card>
<TitleTemplate>
<Text Strong><Icon Type="bulb" />@(item.Questions)</Text>
</TitleTemplate>
<Extra>
<Space>
<SpaceItem>
<a style="color: gray;" @onclick="@(() => OnCopyAsync(item))"><Icon Type="copy" /></a>
</SpaceItem>
<SpaceItem>
<a style="color: gray;" @onclick="@(() => OnClearAsync(item.ID))"><Icon Type="rest" /></a>
</SpaceItem>
</Space>
</Extra>
<Body>
@((MarkupString)(item.HtmlAnswers))
</Body>
</Card>
</GridCol>
</Virtualize>
</div>
<div style="flex-shrink:0;margin:10px;">
<AntDesign.Input @bind-Value="@(_messageInput)" DebounceMilliseconds="@(-1)" Placeholder="输入消息回车发送" OnPressEnter="@(async () => await OnSendAsync())" Disabled="@Sendding">
<Suffix>
<Button Icon="send" Type="@(ButtonType.Link)" OnClick="@(async () => await OnSendAsync())" Disabled="@Sendding"></Button>
</Suffix>
</AntDesign.Input>
</div>
</div>

@code {

}
Loading

0 comments on commit 34c06b1

Please sign in to comment.