Contribute a version of RUNUO2.0:
=============================================
Using System;
Using Server;
Using Server Items;
Using Server Mobiles;
Using System IO;
Using System Net;
Using System Text;
Using System Threading;
Namespace OpenAIBot
{
[CorpseName ("BOT's corpse")]
Public class OpenAIRPC: BaseCreation
{
Private const string OpenAIKey="sk -";
Private const string OpenAIEndpoint=“
https://api.openai.com/v1/chat/completions ";
Private string'botPrompt="Your word";
[Constructible]
Public OpenAIRPC(): base (AIType.AI-Melee, FightMode. None, 10, 1, 0.2, 0.4)
{
Name="OpenAI Bot";
Body=0x190;
Hue=0x83EA;
SpeedHue=0x23F;
//BaseSoundID=0x482;
SetStr (100);
SetDex (100);
SetInt (100);
SetHits (500);
SetStam (100);
SetMana (100);
SetDamage (10, 20);
SetSkill (SkillName. EvalInt, 80.0);
SetSkill (SkillName. Magery, 80.0);
SetSkill (SkillName. MagicResist, 80.0);
}
Public OpenAIRPC (Serial serial): base (serial)
{
}
Public override void OnSpeech (SpeedEventArgs e)
{
Base OnSpeech (e);
If (e.Mobile is PlayerMobile&&! (PlayerMobile) e. Mobile) Deleted
{
String question=e.Speech Trim();
Thread thread=new Thread (new ThreadStart (delegate())
{
Try
{
String response=AskOpenAI (question);
Say (response);
}
Catch (Exception ex)
{
Console WriteLine ("Error in OpenAIRPC. OnSpeech:"+ex. Message);
Console WriteLine ("Error Stack Trace:"+ex. StackTrace);
}
});
Thread Start();
}
}
Private string AskOpenAI (string question)
{
Try
{
HttpWebRequest request=(HttpWebRequest) WebRequest Create (OpenAIEndpoint);
Request Method="POST";
Request Headers Add ("Authorization", "Bearer"+OpenAIKey);
Request ContentType="application/JSON";
Request Timeout=60000;
String requestBody="{" model ":" gpt 3.5 turbo "," messages ": [{" role ":" system "," content ":"+_botPrompt+"\"}, {"role": "user", "content": "+question+" \ "}";
Byte [] requestBodyBytes=Encoding UTF8. GetBytes (requestBody);
Request ContentLength=requestBodyBytes Length;
Using (Stream requestStream=request. GetRequestStream())
{
RequestStream Write (requestBodyBytes, 0, requestBodyBytes. Length);
}
Using (HttpWebResponse response=(HttpWebResponse) request GetResponse()
Using (Stream responseStream=response. GetResponseStream)
Using (StreamReader reader=new StreamReader (responseStream))
{
String responseBody=reader ReadToEnd();
Int contentIndex=responseBody IndexOf (content: );
If (contentIndex!=-1)
{
Int startIndex=contentIndex+. Length;
Int endIndex=responseBody IndexOf ("\", startIndex);
If (endIndex!=-1)
{
String assistantMessage=responseBody Substring (startIndex, endIndex - startIndex);
Return assistantMessage;
}
}
Return "Unable to generate a response.";
}
}
Catch (Exception ex)
{
Console WriteLine ("Exception in OpenAIRPC. AskOpenAI:"+ex.Message);
Console WriteLine ("Exception Stack Trace:"+ex. StackTrace);
Return "An error occurred while generating a response.";
}
}
Public override void Serialize (GenericWriter writer)
{
Base Serialize (writer);
Writer Write ((int) 0)// Version
}
Public override void Deserialize (GenericReader reader)
{
Base Deserialize (reader);
Int version=reader ReadInt();
}
}
}
======================================================================================================
I will report an error because the connection to OPENAI in China is blocked. Is there a big god that can solve the problem of server connection in China? My error message:
Exception in OpenAIRPC AskOpenAI: Basic connection has been closed: An error occurred while sending.
Exception Stack Trace: In System Net HttpWebRequest GetRequestStream (TransportContext&context)
In System Net HttpWebRequest GetRequestStream()
In OpenAIBot OpenAIRPC AskOpenAI (String question)
Exception in OpenAIRPC AskOpenAI: Basic connection has been closed: An error occurred while sending.
Exception Stack Trace: In System Net HttpWebRequest GetRequestStream (TransportContext&context)