Enha: simplier guesser prompt; player team-role recovery
This commit is contained in:
		
							
								
								
									
										153
									
								
								llmapi/main.go
									
									
									
									
									
								
							
							
						
						
									
										153
									
								
								llmapi/main.go
									
									
									
									
									
								
							| @@ -24,7 +24,7 @@ var ( | ||||
| 	MimePrompt = `we are playing alias;\nyou are a mime (player who gives a clue of one noun word and number of cards you expect them to open) of the %s team (people who would guess by your clue want open the %s cards);\nplease return your clue, number of cards to open and what words you mean them to find using that clue in json like:\n{\n\"clue\": \"one-word-noun\",\n\"number\": \"number-from-0-to-9\",\n\"words_I_mean_my_team_to_open\": [\"this\", \"that\", ...]\n}\nthe team who openes all their cards first wins.\nplease return json only.\nunopen Blue cards left: %d;\nunopen Red cards left: %d;\nhere is the game info in json:\n%s` | ||||
| 	// TODO: simplify; bot gets confused; so show it only unrevealed cards and last clue (maybe older clues as well); | ||||
| 	GuesserPrompt       = `we are playing alias;\nyou are to guess words of the %s team (you want open %s cards) by given clue and a number of meant guesses;\nplease return your guesses and words that could be meant by the clue, but you do not wish to open yet, in json like:\n{\n\"guesses\": [\"word1\", \"word2\", ...],\n\"could_be\": [\"this\", \"that\", ...]\n}\nthe team who openes all their cards first wins.\nplease return json only.\nunopen Blue cards left: %d;\nunopen Red cards left: %d;\nhere is the cards (and other info), you need to choose revealed==false words:\n%s` | ||||
| 	GuesserSimplePrompt = `we are playing game of alias;\n you were given a clue: \"%s\";\nnumber of words you can open with that clue is:%s;\nplease return your guesses and words that could be meant by the clue, but you do not wish to open yet, in json like:\n{\n\"guesses\": [\"word1\", \"word2\", ...],\n\"could_be\": [\"this\", \"that\", ...]\n}\nhere is the words that left:\n%s` | ||||
| 	GuesserSimplePrompt = `we are playing game of alias;\n you were given a clue: \"%s\";\nplease return your guess and words that could be meant by the clue, but you do not wish to open yet, in json like:\n{\n\"guess\": "most_relevant_word_to_the_clue",\n\"could_be\": [\"this\", \"that\", ...]\n}\nhere is the words that left:\n%s` | ||||
| ) | ||||
|  | ||||
| type DSResp struct { | ||||
| @@ -103,11 +103,79 @@ func convertToSliceOfStrings(value any) ([]string, error) { | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func (b *Bot) checkGuesses(tempMap map[string]any, room *models.Room) error { | ||||
| 	guesses, err := convertToSliceOfStrings(tempMap["guesses"]) | ||||
| 	if err != nil { | ||||
| 		b.log.Warn("failed to parse bot given guesses", "mimeResp", tempMap, "bot_name", b.BotName) | ||||
| 		return err | ||||
| 	} | ||||
| 	for _, word := range guesses { | ||||
| 		if err := b.checkGuess(word, room); err != nil { | ||||
| 			// log error | ||||
| 			b.log.Warn("failed to check guess", "mimeResp", tempMap, "bot_name", b.BotName, "guess", word) | ||||
| 			return err | ||||
| 		} | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (b *Bot) checkGuess(word string, room *models.Room) error { | ||||
| 	color, exists := room.WCMap[word] | ||||
| 	b.log.Debug("bot trying to open card", "word", word, "color", | ||||
| 		color, "exists", exists) | ||||
| 	if !exists { | ||||
| 		return fmt.Errorf("fn: checkGuess; %s does not exists", word) | ||||
| 	} | ||||
| 	room.RevealSpecificWord(word) | ||||
| 	room.UpdateCounter() | ||||
| 	action := models.Action{ | ||||
| 		Actor:      b.BotName, | ||||
| 		ActorColor: b.Team, | ||||
| 		WordColor:  string(color), | ||||
| 		Action:     "guessed", | ||||
| 		Word:       word, | ||||
| 	} | ||||
| 	room.ActionHistory = append(room.ActionHistory, action) | ||||
| 	// if opened card is of color of opp team, change turn | ||||
| 	oppositeColor := room.GetOppositeTeamColor() | ||||
| 	switch string(color) { | ||||
| 	case "black": | ||||
| 		// game over | ||||
| 		room.IsRunning = false | ||||
| 		room.IsOver = true | ||||
| 		room.TeamWon = oppositeColor | ||||
| 	case "white", string(oppositeColor): | ||||
| 		// end turn | ||||
| 		room.TeamTurn = oppositeColor | ||||
| 		room.MimeDone = false | ||||
| 	} | ||||
| 	// check if no cards left => game over | ||||
| 	if room.BlueCounter == 0 { | ||||
| 		// blue won | ||||
| 		room.IsRunning = false | ||||
| 		room.IsOver = true | ||||
| 		room.TeamWon = "blue" | ||||
| 	} | ||||
| 	if room.RedCounter == 0 { | ||||
| 		// red won | ||||
| 		room.IsRunning = false | ||||
| 		room.IsOver = true | ||||
| 		room.TeamWon = "red" | ||||
| 	} | ||||
| 	if err := saveRoom(room); err != nil { | ||||
| 		b.log.Error("failed to save room", "room", room) | ||||
| 		err = fmt.Errorf("fn: checkGuess, failed to save room; err: %w", err) | ||||
| 		return err | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // StartBot | ||||
| func (b *Bot) StartBot() { | ||||
| 	for { | ||||
| 		select { | ||||
| 		case <-SignalChanMap[b.BotName]: | ||||
| 			botJournalName := models.NotifyJournalPrefix + b.RoomID | ||||
| 			b.log.Debug("got signal", "bot-team", b.Team, "bot-role", b.Role) | ||||
| 			// get room cards and actions | ||||
| 			room, err := getRoomByID(b.RoomID) | ||||
| @@ -118,14 +186,26 @@ func (b *Bot) StartBot() { | ||||
| 			// form prompt | ||||
| 			prompt := b.BuildPrompt(room) | ||||
| 			b.log.Debug("got prompt", "prompt", prompt) | ||||
| 			broker.Notifier.Notifier <- broker.NotificationEvent{ | ||||
| 				EventName: botJournalName, | ||||
| 				Payload:   prompt, | ||||
| 			} | ||||
| 			// call llm | ||||
| 			llmResp, err := b.CallLLM(prompt) | ||||
| 			if err != nil { | ||||
| 				broker.Notifier.Notifier <- broker.NotificationEvent{ | ||||
| 					EventName: botJournalName, | ||||
| 					Payload:   "failed to get bot resp", | ||||
| 				} | ||||
| 				b.log.Error("bot loop", "error", err) | ||||
| 				continue | ||||
| 			} | ||||
| 			tempMap, err := b.LLMParser.ParseBytes(llmResp) | ||||
| 			if err != nil { | ||||
| 				broker.Notifier.Notifier <- broker.NotificationEvent{ | ||||
| 					EventName: botJournalName, | ||||
| 					Payload:   "failed to parse bot resp", | ||||
| 				} | ||||
| 				b.log.Error("bot loop", "error", err, "resp", string(llmResp)) | ||||
| 				continue | ||||
| 			} | ||||
| @@ -154,60 +234,24 @@ func (b *Bot) StartBot() { | ||||
| 				} | ||||
| 				room.ThisTurnLimit = uint8(guessLimitU64) | ||||
| 			case models.UserRoleGuesser: | ||||
| 				guesses, err := convertToSliceOfStrings(tempMap["guesses"]) | ||||
| 				if err != nil { | ||||
| 					b.log.Warn("failed to parse bot given guesses", "mimeResp", tempMap, "bot_name", b.BotName) | ||||
| 					continue | ||||
| 				// // deprecated | ||||
| 				// if err := b.checkGuesses(tempMap, room); err != nil { | ||||
| 				// 	b.log.Warn("failed to check guess", "mimeResp", tempMap, "bot_name", b.BotName) | ||||
| 				// 	continue | ||||
| 				// } | ||||
| 				guess, ok := tempMap["guess"].(string) | ||||
| 				if !ok || guess == "" { | ||||
| 					b.log.Warn("failed to parse guess", "mimeResp", tempMap, "bot_name", b.BotName) | ||||
| 				} | ||||
| 				for _, word := range guesses { | ||||
| 					color, exists := room.WCMap[word] | ||||
| 					b.log.Debug("bot trying to open card", "word", word, "color", | ||||
| 						color, "exists", exists) | ||||
| 					if !exists { | ||||
| 						continue | ||||
| 					} | ||||
| 					room.RevealSpecificWord(word) | ||||
| 					room.UpdateCounter() | ||||
| 					action := models.Action{ | ||||
| 						Actor:      b.BotName, | ||||
| 						ActorColor: b.Team, | ||||
| 						WordColor:  string(color), | ||||
| 						Action:     "guessed", | ||||
| 						Word:       word, | ||||
| 					} | ||||
| 					room.ActionHistory = append(room.ActionHistory, action) | ||||
| 					// if opened card is of color of opp team, change turn | ||||
| 					oppositeColor := room.GetOppositeTeamColor() | ||||
| 					switch string(color) { | ||||
| 					case "black": | ||||
| 						// game over | ||||
| 						room.IsRunning = false | ||||
| 						room.IsOver = true | ||||
| 						room.TeamWon = oppositeColor | ||||
| 					case "white", string(oppositeColor): | ||||
| 						// end turn | ||||
| 						room.TeamTurn = oppositeColor | ||||
| 						room.MimeDone = false | ||||
| 					} | ||||
| 					// check if no cards left => game over | ||||
| 					if room.BlueCounter == 0 { | ||||
| 						// blue won | ||||
| 						room.IsRunning = false | ||||
| 						room.IsOver = true | ||||
| 						room.TeamWon = "blue" | ||||
| 					} | ||||
| 					if room.RedCounter == 0 { | ||||
| 						// red won | ||||
| 						room.IsRunning = false | ||||
| 						room.IsOver = true | ||||
| 						room.TeamWon = "red" | ||||
| 					} | ||||
| 					if err := saveRoom(room); err != nil { | ||||
| 						b.log.Error("failed to save room", "room", room) | ||||
| 						continue | ||||
| 					} | ||||
| 				if err := b.checkGuess(guess, room); err != nil { | ||||
| 					b.log.Warn("failed to check guess", "mimeResp", tempMap, "bot_name", b.BotName, "guess", guess) | ||||
| 				} | ||||
| 				b.log.Info("mime resp log", "guesserResp", tempMap) | ||||
| 				couldBe, err := convertToSliceOfStrings(tempMap["could_be"]) | ||||
| 				if err != nil { | ||||
| 					b.log.Warn("failed to parse could_be", "bot_resp", tempMap, "bot_name", b.BotName) | ||||
| 				} | ||||
| 				room.LogJournal = append(room.LogJournal, fmt.Sprintf("%s also considered this: %v", b.BotName, couldBe)) | ||||
| 				eventName = models.NotifyRoomUpdatePrefix + room.ID | ||||
| 				eventPayload = "" | ||||
| 			default: | ||||
| @@ -262,7 +306,7 @@ func NewBot(role, team, name, roomID string, cfg *config.Config, recovery bool) | ||||
| 	if role == "mime" && room.IsRunning && !recovery { | ||||
| 		return nil, errors.New("cannot join after game started") | ||||
| 	} | ||||
| 	room.PlayerList = append(room.PlayerList, name) | ||||
| 	// room.PlayerList = append(room.PlayerList, name) | ||||
| 	bp := models.BotPlayer{ | ||||
| 		Role: models.StrToUserRole(role), | ||||
| 		Team: models.StrToUserTeam(team), | ||||
| @@ -343,6 +387,9 @@ func (b *Bot) BuildSimpleGuesserPrompt(room *models.Room) string { | ||||
| 	clue := room.ActionHistory[len(room.ActionHistory)-1].Word | ||||
| 	number := room.ActionHistory[len(room.ActionHistory)-1].Number | ||||
| 	words := make([]string, len(room.Cards)) | ||||
| 	for i, card := range room.Cards { | ||||
| 		words[i] = card.Word | ||||
| 	} | ||||
| 	return fmt.Sprintf(GuesserSimplePrompt, clue, number, words) | ||||
| } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Grail Finder
					Grail Finder