Skip to content

Instantly share code, notes, and snippets.

@dfinke
Created August 29, 2023 21:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dfinke/b906fb93ecef892615cd70d591052dd6 to your computer and use it in GitHub Desktop.
Save dfinke/b906fb93ecef892615cd70d591052dd6 to your computer and use it in GitHub Desktop.
Simulates a conversation between two bots
class Bot {
[string] $Convo
[string] $Name
[string] $Context
[object] $Client
[object[]] $History
Bot([string] $convo, [string] $name, [string] $context, [object] $client, [object[]] $history) {
$this.Convo = $convo
$this.Name = $name
$this.Context = $context
$this.Client = $client
$this.History = $history
}
[string] Chat([string] $from, [string] $message) {
$messages = $this.History + (New-ChatMessageTemplate -Name $from.Replace(" ", "") -Role user -Content $message)
$resp = Get-ChatCompletion -messages $messages
# Update the bot history with the response.
$this.History += $resp.Choices[0].Message
# Fetch and return the reply.
return $resp.Choices[0].Message.Content
}
[void] InjectContext([string] $context) {
# Inject a "system" message generated based on the conversation and bot context.
$message = New-ChatMessageTemplate -Role system -Content "From this point onwards in the conversation, please keep this information in mind: $context"
$this.History += $message
}
}
function New-Bot {
param(
[string]$convo,
[string]$name,
[string]$context
)
$history = @(
New-ChatMessageTemplate -Role system -Content "You are about to have a conversation. To prepare you, here is an overview of what that conversation is expected to entail: $convo."
New-ChatMessageTemplate -Role system -Content "You have a personality. Your name is $name, and you have the following key personal, background, and stylistic traits which your responses should be consistent with: $context."
New-ChatMessageTemplate -Role system -Content "All of your replies should be from your perspective and should be a single person's response as though you are actually having a conversation with another individual."
)
return [bot]::new($convo, $name, $context, $null, $history)
}
# Prompt the user for a few things before firing off the conversation.
$convo = Read-Host "Enter the background/context about this ensuing conversation"
# Ask for personal and/or stylistic information about each bot.
$bots = @()
$botColors = @("Green", "DarkCyan")
for ($i = 0; $i -lt 2; $i++) {
$botName = Read-Host "Tell me Bot#$($i+1)'s name"
$botContext = Read-Host "Tell me key personal, background, or stylistic information about $botName"
# Create a new bot and add it to the list of bots.
$bots += New-Bot -convo $convo -name $botName -context $botContext
}
# Now that we have the bots, we can start the conversation. Ask for the starting
# point for the conversation, which Bot#1 will say to Bot#2:
Write-Host "--------------------------------------------------"
Write-Host "Let's start the conversation!"
Write-Host -ForegroundColor $botColors[0] "$($bots[0].Name) "
$lastMessage = Read-Host
# Now loop and keep the conversation going; if the user wants to escape, they can
# hit ^C, otherwise hitting <ENTER> will keep the conversation going.
$turn = 0
while ($true) {
# We alternate turns between the bots.
$asker = $bots[$turn]
$replier = $bots[1 - $turn]
Write-Host "--------------------------------------------------"
# The asker now asks a question of the replier.
$reply = $replier.Chat($asker.Name, $lastMessage)
Write-Host -ForegroundColor $botColors[1 - $turn] "$($replier.Name): " -NoNewline
Write-Host $reply
# See if the user wants to keep going.
$addedContext = Read-Host "[<ENTER> to continue; ^C to quit]`n[Feel free to inject new conversation context before <ENTER> "
if ($addedContext -ne "") {
# If there is new conversation context, inject it into both bots.
$asker.InjectContext($addedContext)
$replier.InjectContext($addedContext)
}
# If we're continuing onwards, swap the asker and replier, and keep going!
$turn = 1 - $turn
$lastMessage = $reply
}
@dfinke
Copy link
Author

dfinke commented Aug 29, 2023

PSConvoBot

ported from: https://github.com/joeduffy/convogpt/tree/master

Bots talking to bots.

When you run PSConvoBot, it will ask you some questions:

  • Enter the background/context about this ensuing conversation.
  • Tell me Bot#1's name.
  • Tell me key personal, background, or stylistic information about (Bot#1 name).
  • Tell me Bot#2's name.
  • Tell me key personal, background, or stylistic information about (Bot#2 name).

From there, it will prompt you to impersonate Bot#1 and say something to Bot#2. Afterwards, the two bots
will continue to interact until you stop it.

After each interaction, you'll be given the opportunity to hit ENTER to continue, or ^C to stop. And you can
add additional contextual information to the conversation going forward by typing that context before hitting ENTER.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment