Skip to content

Commit

Permalink
add relationships back
Browse files Browse the repository at this point in the history
  • Loading branch information
ianmacartney committed Aug 14, 2023
1 parent 8749de6 commit 27e8451
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
13 changes: 5 additions & 8 deletions convex/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ import {
import { getNearbyPlayers } from './lib/physics';
import { CONVERSATION_TIME_LIMIT, CONVERSATION_PAUSE } from './config';

const awaitTimeout = (delay: number) =>
new Promise(resolve => setTimeout(resolve, delay));

const awaitTimeout = (delay: number) => new Promise((resolve) => setTimeout(resolve, delay));

export const runAgentBatch = internalAction({
args: {
Expand Down Expand Up @@ -175,7 +173,7 @@ export async function handleAgentInteraction(
const relationshipsByPlayerId = new Map(
relations.map(({ playerId, relations }) => [
playerId,
relations.map((r) => ({ ...playerById.get(playerId)!, relationship: r.relationship })),
relations.map((r) => ({ ...playerById.get(r.id)!, relationship: r.relationship })),
]),
);

Expand All @@ -198,7 +196,6 @@ export async function handleAgentInteraction(
chatHistory,
);
lastSpeakerId = speaker.id;
const audiencePlayers = players.filter((p) => p.id !== speaker.id);
const audience = players.filter((p) => p.id !== speaker.id).map((p) => p.id);
const shouldWalkAway = audience.length === 0 || (await walkAway(chatHistory, speaker));

Expand All @@ -223,10 +220,10 @@ export async function handleAgentInteraction(
const playerRelations = relationshipsByPlayerId.get(speaker.id) ?? [];
let playerCompletion;
if (messages.length === 0) {
playerCompletion = await startConversation(ctx, audiencePlayers, memory, speaker);
playerCompletion = await startConversation(ctx, playerRelations, memory, speaker);
} else {
// TODO: stream the response and write to the mutation for every sentence.
playerCompletion = await converse(ctx, chatHistory, speaker, audiencePlayers, memory);
playerCompletion = await converse(ctx, chatHistory, speaker, playerRelations, memory);
}

const message = await ctx.runMutation(internal.journal.talk, {
Expand All @@ -242,7 +239,7 @@ export async function handleAgentInteraction(
}

// slow down conversations
await awaitTimeout(CONVERSATION_PAUSE);
await awaitTimeout(CONVERSATION_PAUSE);
}

if (messages.length > 0) {
Expand Down
12 changes: 8 additions & 4 deletions convex/conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { LLMMessage, chatCompletion, fetchEmbedding } from './lib/openai';
import { Message } from './schema';

type Player = { id: Id<'players'>; name: string; identity: string };
type Relation = Player & { relationship: string };
type Relation = Player & { relationship?: string };

export async function startConversation(
ctx: ActionCtx,
audience: Player[],
audience: Relation[],
memory: MemoryDB,
player: Player,
) {
Expand All @@ -30,7 +30,10 @@ export async function startConversation(
role: 'user',
content:
`You are ${player.name}. You just saw ${newFriendsNames}. You should greet them and start a conversation with them. Below are some of your memories about ${newFriendsNames}:` +
//relationships.map((r) => r.relationship).join('\n') +
audience
.filter((r) => r.relationship)
.map((r) => `Relationship with ${r.name}: ${r.relationship}`)
.join('\n') +
convoMemories.map((r) => r.memory.description).join('\n') +
`\n${player.name}:`,
},
Expand Down Expand Up @@ -103,7 +106,7 @@ export async function converse(
ctx: ActionCtx,
messages: LLMMessage[],
player: Player,
nearbyPlayers: Player[],
nearbyPlayers: Relation[],
memory: MemoryDB,
) {
const nearbyPlayersNames = nearbyPlayers.join(', ');
Expand Down Expand Up @@ -136,6 +139,7 @@ export async function converse(

nearbyPlayers.forEach((p) => {
prefixPrompt += `\nAbout ${p.name}: ${p.identity}\n`;
if (p.relationship) prefixPrompt += `Relationship with ${p.name}: ${p.relationship}\n`;
});

prefixPrompt += `Last time you chatted with some of ${nearbyPlayersNames} it was ${lastConversationTs}. It's now ${Date.now()}. You can cut this conversation short if you talked to this group of people within the last day. \n}`;
Expand Down
2 changes: 1 addition & 1 deletion convex/journal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export const getRelationships = internalQuery({
const relationship = await latestRelationshipMemoryWith(ctx.db, playerId, otherPlayerId);
return {
id: otherPlayerId,
relationship: relationship?.description ?? 'unknown',
relationship: relationship?.description,
};
}),
};
Expand Down

0 comments on commit 27e8451

Please sign in to comment.