Skip to content

Instantly share code, notes, and snippets.

@pjmagee
Created April 29, 2023 15:30
Show Gist options
  • Select an option

  • Save pjmagee/674ba43bcd37925fdb35427fed92999f to your computer and use it in GitHub Desktop.

Select an option

Save pjmagee/674ba43bcd37925fdb35427fed92999f to your computer and use it in GitHub Desktop.
ChatGPT R&D
Character character = new Character();
var serializeOptions = new JsonSerializerOptions { WriteIndented = true, Converters = { new JsonStringEnumConverter() } };
var api = new OpenAIAPI(Util.GetPassword("openapi.apikey"));
var systemMessages = new List<string>()
{
"You only respond in JSON",
"You do not respond in normal test",
"Read the user input and fill in the blanks",
"You are the game master for a users character in the star wars universe",
"The user will make choices which you understand from the input userAction field",
"You understand the userAction field and produce the missing Output fields in the response",
"You will provide choices for the user based on the users last action and their characters current situation"
};
string input = string.Empty;
Conversation convo = api.Chat.CreateConversation(new() { Model = Model.ChatGPTTurbo, Temperature = 0.1, });
systemMessages.ForEach(message => convo.AppendSystemMessage(message));
// convo.AppendExampleChatbotOutput();
Dictionary<string, object> characterSheet = new Dictionary<string, object>();
characterSheet["character sheet"] = character;
convo.AppendSystemMessage(JsonSerializer.Serialize(characterSheet, serializeOptions).Dump());
// examples.ForEach(message => convo.AppendExampleChatbotOutput(message));
if (character.IsNew)
{
var welcomePrompt =
$"""
Generate a short intro paragraph for the player {character.Name} about their character's background.
The story should take into account the character class {character.Class} and {character.Alignment} alignment.
The story should lead them up into a situation where they must find their ship.
""";
var completionResponse = await api.Completions.CreateCompletionAsync(new CompletionRequest { Prompt = welcomePrompt, MaxTokens = 500, Model = Model.DefaultModel });
completionResponse.Completions[0].Text.Dump();
character.StoryLog.Add(new CharacterStoryLog()
{
Story = completionResponse.Completions[0].Text,
CharacterAction = new() { Description = "Where am i?", EthicalImpact = "None", Points = 0 }
});
character.IsNew = false;
}
do
{
input = await Util.ReadLineAsync();
bool sendRequest = !string.IsNullOrWhiteSpace(input) && !input.Equals("quit");
if (sendRequest)
{
var request = new Request
{
Input = new ()
{
UserAction = input
},
Output = new ()
{
CharacterActionFromInput = new()
{
Description = "",
EthicalImpact = "",
Points = 0
},
CharacterStoryPrompt = new CharacterStoryPrompt()
{
AvailableActions = new List<CharacterAction>()
{
new CharacterAction() { Description = "", EthicalImpact = "", Points = 0 },
new CharacterAction() { Description = "", EthicalImpact = "", Points = 0 },
new CharacterAction() { Description = "", EthicalImpact = "", Points = 0 },
},
Story = "",
}
}
};
convo.AppendUserInput(System.Text.Json.JsonSerializer.Serialize(request, new JsonSerializerOptions() { WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.Never } ));
var chatResponse = await convo.GetResponseFromChatbotAsync();
var response = JsonSerializer.Deserialize<Request>(chatResponse);
if (response.Output.CharacterActionIsValid)
{
character.StoryLog.Add(new CharacterStoryLog
{
CharacterAction = response.Output.CharacterActionFromInput,
Story = response.Output.CharacterStoryPrompt.Story,
});
response.Output.CharacterStoryPrompt.Dump("What will your character do next?");
}
character.Dump();
}
}
while(input != "quit");
public class Request
{
public InputParameters Input { get; set; }
public OutputParameters Output { get; set; }
public Request()
{
Input = new InputParameters();
Output = new OutputParameters();
}
public class InputParameters
{
public string UserAction { get; set; }
}
public class OutputParameters
{
public bool CharacterActionIsValid { get; set; }
public CharacterAction CharacterActionFromInput { get; set; }
public CharacterStoryPrompt CharacterStoryPrompt { get; set; }
}
}
public class Response
{
}
public class Quest
{
public string Name { get; set; }
public string Description { get; set; }
public int XP { get; set; }
public int Credits { get; set; }
public string[] Items { get; set; }
public bool Completed { get; set; }
}
public class Character
{
public List<CharacterStoryLog> StoryLog { get; set; }
public bool IsNew { get; set; } = true;
public string Name { get; set; } = "Delegate";
public string Gender { get; set; } = "Male";
public string Alignment { get; set; } = "Unlawful Dark side";
public int Level { get; set; } = 1;
public int XP { get; set; } = 0;
public int Dexterity { get; set; } = 1;
public int Strength { get; set; } = 1;
public int Constitution { get; set; } = 1;
public int Wisdom { get; set; } = 1;
public int Charisma { get; set; } = 1;
public string Class { get; set; } = "Berserker";
public int Credits { get; set; } = 500;
public List<string> Items { get; set; } = new();
public List<string> Feats { get; set; } = new();
public List<string> ClassAbilities { get; set; } = new();
public string Weapon { get; set; } = "";
public string Armor { get; set; } = "";
public string Zone { get; set; } = "Dantooine";
public string Location { get; set; } = "Bacta Tank";
public Quests Quests { get; set; }
public Character()
{
StoryLog = new List<CharacterStoryLog>();
Quests = new();
Quests.Active.Add(new Quest()
{
Completed = false,
Credits = 1000,
Name = "Find my starship",
Description = "I must find my Starship so I can get back to my duties of being a Sith",
XP = 1000,
Items = new[] { "medpack", "hunting knife", "datapad" }
});
}
}
public class Quests
{
public List<Quest> Completed { get; set; }
public List<Quest> Active { get; set; }
public Quests()
{
Completed = new();
Active = new();
}
}
public class CharacterStoryPrompt
{
public string Story { get; set; } = "";
public List<CharacterAction> AvailableActions { get; set; }
}
public class CharacterAction
{
public string Description { get; set; } = "";
public string EthicalImpact { get; set; } = "";
public int Points { get; set; } = 0;
}
public class CharacterStoryLog
{
public string Story { get; set; }
public CharacterAction CharacterAction { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment