add llm ai
This commit is contained in:
@@ -2,6 +2,7 @@ from __future__ import annotations
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import TYPE_CHECKING
|
||||
import random
|
||||
import json
|
||||
|
||||
from src.classes.essence import Essence, EssenceType
|
||||
from src.classes.root import Root, corres_essence_type
|
||||
@@ -48,6 +49,7 @@ class Move(DefineAction):
|
||||
"""
|
||||
最基础的移动动作,在tile之间进行切换。
|
||||
"""
|
||||
COMMENT = "移动到某个相对位置"
|
||||
def execute(self, delta_x: int, delta_y: int) -> Event|NullEvent:
|
||||
"""
|
||||
移动到某个tile
|
||||
@@ -71,10 +73,13 @@ class MoveToRegion(DefineAction):
|
||||
"""
|
||||
移动到某个region
|
||||
"""
|
||||
def execute(self, region: Region) -> Event|NullEvent:
|
||||
COMMENT = "移动到某个区域"
|
||||
def execute(self, region: Region|str) -> Event|NullEvent:
|
||||
"""
|
||||
移动到某个region
|
||||
"""
|
||||
if isinstance(region, str):
|
||||
region = self.world.map.region_names[region]
|
||||
cur_loc = (self.avatar.pos_x, self.avatar.pos_y)
|
||||
region_center_loc = region.center_loc
|
||||
delta_x = region_center_loc[0] - cur_loc[0]
|
||||
@@ -89,6 +94,7 @@ class Cultivate(DefineAction):
|
||||
"""
|
||||
修炼动作,可以增加修仙进度。
|
||||
"""
|
||||
COMMENT = "修炼,增进修为"
|
||||
def execute(self) -> Event|NullEvent:
|
||||
"""
|
||||
修炼
|
||||
@@ -116,6 +122,7 @@ class Breakthrough(DefineAction):
|
||||
"""
|
||||
突破境界
|
||||
"""
|
||||
COMMENT = "尝试突破境界"
|
||||
def calc_success_rate(self) -> float:
|
||||
"""
|
||||
计算突破境界的成功率
|
||||
@@ -137,4 +144,12 @@ class Breakthrough(DefineAction):
|
||||
return Event(self.world.year, self.world.month, f"{self.avatar.name} 突破境界{res}")
|
||||
|
||||
|
||||
ALL_ACTION_CLASSES = [Move, Cultivate, Breakthrough, MoveToRegion]
|
||||
ALL_ACTION_CLASSES = [Move, Cultivate, Breakthrough, MoveToRegion]
|
||||
# 不包括Move
|
||||
ACTION_SPACE = [
|
||||
# {"action": "Move", "params": {"delta_x": int, "delta_y": int}, "comment": Move.COMMENT},
|
||||
{"action": "Cultivate", "params": {}, "comment": Cultivate.COMMENT},
|
||||
{"action": "Breakthrough", "params": {}, "comment": Breakthrough.COMMENT},
|
||||
{"action": "MoveToRegion", "params": {"region": "region_name"}, "comment": MoveToRegion.COMMENT},
|
||||
]
|
||||
ACTION_SPACE_STR = json.dumps(ACTION_SPACE, ensure_ascii=False)
|
||||
@@ -8,6 +8,8 @@ from abc import ABC, abstractmethod
|
||||
from src.classes.world import World
|
||||
from src.classes.tile import Region
|
||||
from src.classes.root import corres_essence_type
|
||||
from src.classes.action import ACTION_SPACE_STR
|
||||
from src.utils.llm import get_ai_prompt_and_call_llm
|
||||
|
||||
class AI(ABC):
|
||||
"""
|
||||
@@ -23,12 +25,6 @@ class AI(ABC):
|
||||
"""
|
||||
pass
|
||||
|
||||
# def create_event(self, world: World, content: str) -> Event:
|
||||
# """
|
||||
# 创建事件
|
||||
# """
|
||||
# return Event(world.year, world.month, content)
|
||||
|
||||
class RuleAI(AI):
|
||||
"""
|
||||
规则AI
|
||||
@@ -69,4 +65,14 @@ class LLMAI(AI):
|
||||
"""
|
||||
决定做什么
|
||||
"""
|
||||
pass
|
||||
action_space_str = ACTION_SPACE_STR
|
||||
avatar_infos_str = str(self.avatar)
|
||||
regions_str = "\n".join([str(region) for region in world.map.regions.values()])
|
||||
dict_info = {
|
||||
"action_space": action_space_str,
|
||||
"avatar_infos": avatar_infos_str,
|
||||
"regions": regions_str
|
||||
}
|
||||
res = get_ai_prompt_and_call_llm(dict_info)
|
||||
action_name, action_params = res["action_name"], res["action_params"]
|
||||
return action_name, action_params
|
||||
@@ -12,7 +12,7 @@ from src.classes.cultivation import CultivationProgress, Realm
|
||||
from src.classes.root import Root
|
||||
from src.classes.age import Age
|
||||
from src.utils.strings import to_snake_case
|
||||
from src.classes.ai import AI, RuleAI
|
||||
from src.classes.ai import AI, RuleAI, LLMAI
|
||||
|
||||
class Gender(Enum):
|
||||
MALE = "male"
|
||||
@@ -52,9 +52,17 @@ class Avatar:
|
||||
在Avatar创建后自动绑定基础动作和AI
|
||||
"""
|
||||
self.tile = self.world.map.get_tile(self.pos_x, self.pos_y)
|
||||
self.ai = RuleAI(self)
|
||||
self.ai = LLMAI(self)
|
||||
# self.ai = RuleAI(self)
|
||||
self._bind_basic_actions()
|
||||
|
||||
def __str__(self) -> str:
|
||||
"""
|
||||
获取avatar的详细信息
|
||||
尽量多打一些,因为会用来给LLM进行决策
|
||||
"""
|
||||
return f"Avatar(id={self.id}, 性别={self.gender}, 年龄={self.age}, name={self.name}, 区域={self.tile.region.name}, 灵根={self.root.value}, 境界={self.cultivation_progress})"
|
||||
|
||||
def _bind_basic_actions(self):
|
||||
"""
|
||||
绑定基础动作,如移动等
|
||||
|
||||
@@ -148,4 +148,7 @@ class CultivationProgress:
|
||||
"""
|
||||
检查是否可以突破
|
||||
"""
|
||||
return self.level in level_to_break_through.keys()
|
||||
return self.level in level_to_break_through.keys()
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"{self.realm.value}{self.stage.value}({self.level}级)。可以突破:{self.can_break_through()}"
|
||||
@@ -45,6 +45,15 @@ class Region():
|
||||
def __post_init__(self):
|
||||
self.id = next(region_id_counter)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"区域。名字:{self.name},描述:{self.description},最浓的灵气:{self.get_most_dense_essence()}, 灵气值:{self.get_most_dense_essence_value()}"
|
||||
|
||||
def get_most_dense_essence(self) -> EssenceType:
|
||||
return max(self.essence.density.items(), key=lambda x: x[1])[0]
|
||||
|
||||
def get_most_dense_essence_value(self) -> int:
|
||||
most_dense_essence = self.get_most_dense_essence()
|
||||
return self.essence.density[most_dense_essence]
|
||||
|
||||
def __hash__(self) -> int:
|
||||
return hash(self.id)
|
||||
@@ -74,7 +83,8 @@ class Map():
|
||||
"""
|
||||
def __init__(self, width: int, height: int):
|
||||
self.tiles = {}
|
||||
self.regions = {}
|
||||
self.regions = {} # region_id -> region
|
||||
self.region_names = {} # region_name -> region
|
||||
self.width = width
|
||||
self.height = height
|
||||
|
||||
@@ -101,6 +111,7 @@ class Map():
|
||||
region.center_loc = center_loc
|
||||
region.area = len(locs)
|
||||
self.regions[region.id] = region
|
||||
self.region_names[name] = region
|
||||
return region
|
||||
|
||||
def get_center_locs(self, locs: list[tuple[int, int]]) -> tuple[int, int]:
|
||||
|
||||
Reference in New Issue
Block a user