create AI AGENT USING ASPX PAGE
https://platform.openai.com/login?next=%2Fapi-keys%3Futm_source%3Dchatgpt.com
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
https://platform.openai.com/api-keys?utm_source=chatgpt.com
<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="AIAgent.aspx.cs"
Inherits="MyAIApp.AIAgent" %>
<!DOCTYPE html>
<html>
<head runat="server">
<title>AI Agent</title>
<style>
body {
font-family: Arial;
background: #f5f7fa;
}
.chatbox {
width: 700px;
margin: 50px auto;
background: white;
padding: 25px;
border-radius: 10px;
box-shadow: 0 0 10px #ccc;
}
.question {
width: 100%;
height: 100px;
padding: 10px;
}
.btn {
margin-top: 15px;
padding: 12px 25px;
background: #0078d4;
color: white;
border: none;
cursor: pointer;
}
.answer {
margin-top: 20px;
padding: 15px;
background: #f1f1f1;
white-space: pre-wrap;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="chatbox">
<h2>🤖 NRsolution4u AI Agent</h2>
<asp:TextBox
ID="txtQuestion"
runat="server"
CssClass="question"
TextMode="MultiLine"
placeholder="Ask something...">
</asp:TextBox>
<br />
<asp:Button
ID="btnAsk"
runat="server"
Text="Ask AI"
CssClass="btn"
OnClick="btnAsk_Click" />
<asp:Label
ID="lblAnswer"
runat="server"
CssClass="answer">
</asp:Label>
</div>
</form>
</body>
</html>
====================================================================
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace MyAIApp
{
public partial class AIAgent : System.Web.UI.Page
{
protected async void btnAsk_Click(object sender, EventArgs e)
{
string question = txtQuestion.Text.Trim();
if (string.IsNullOrEmpty(question))
{
lblAnswer.Text = "Please enter your question.";
return;
}
try
{
string answer = await AskAI(question);
lblAnswer.Text = Server.HtmlEncode(answer)
.Replace("\r\n", "<br/>")
.Replace("\n", "<br/>");
}
catch (Exception ex)
{
lblAnswer.Text = "Error: " + Server.HtmlEncode(ex.Message);
}
}
private async Task<string> AskAI(string question)
{
string apiKey = "YOUR_API_KEY";
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Add(
"Authorization",
"Bearer " + apiKey);
var request = new
{
model = "YOUR_MODEL",
input = question
};
string json = JsonConvert.SerializeObject(request);
StringContent content = new StringContent(
json,
Encoding.UTF8,
"application/json");
HttpResponseMessage response =
await client.PostAsync(
"YOUR_AI_API_ENDPOINT",
content);
string result =
await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
{
throw new Exception(result);
}
JObject obj = JObject.Parse(result);
return obj.ToString();
}
}
}
}
Comments
Post a Comment