prompt-based agent that emits action given the history
| 149 | |
| 150 | |
| 151 | class PromptAgent(Agent): |
| 152 | """prompt-based agent that emits action given the history""" |
| 153 | |
| 154 | def __init__( |
| 155 | self, |
| 156 | action_set_tag: str, |
| 157 | lm_config: lm_config.LMConfig, |
| 158 | prompt_constructor: PromptConstructor, |
| 159 | ) -> None: |
| 160 | super().__init__() |
| 161 | self.lm_config = lm_config |
| 162 | self.prompt_constructor = prompt_constructor |
| 163 | self.action_set_tag = action_set_tag |
| 164 | |
| 165 | @beartype |
| 166 | def set_action_set_tag(self, tag: str) -> None: |
| 167 | self.action_set_tag = tag |
| 168 | |
| 169 | # @beartype |
| 170 | def next_action( |
| 171 | self, trajectory: Trajectory, intent: str, meta_data: dict[str, Any] |
| 172 | ) -> (Action, str, str): |
| 173 | prompt = self.prompt_constructor.construct( |
| 174 | trajectory, intent, meta_data |
| 175 | ) |
| 176 | lm_config = self.lm_config |
| 177 | if lm_config.provider == "openai": |
| 178 | if lm_config.mode == "chat": |
| 179 | response = generate_from_openai_chat_completion( |
| 180 | messages=prompt, |
| 181 | model=lm_config.model, |
| 182 | temperature=lm_config.gen_config["temperature"], |
| 183 | top_p=lm_config.gen_config["top_p"], |
| 184 | context_length=lm_config.gen_config["context_length"], |
| 185 | max_tokens=lm_config.gen_config["max_tokens"], |
| 186 | stop_token=None, |
| 187 | ) |
| 188 | elif lm_config.mode == "completion": |
| 189 | response = generate_from_openai_completion( |
| 190 | prompt=prompt, |
| 191 | engine=lm_config.model, |
| 192 | temperature=lm_config.gen_config["temperature"], |
| 193 | max_tokens=lm_config.gen_config["max_tokens"], |
| 194 | top_p=lm_config.gen_config["top_p"], |
| 195 | stop_token=lm_config.gen_config["stop_token"], |
| 196 | ) |
| 197 | else: |
| 198 | raise ValueError( |
| 199 | f"OpenAI models do not support mode {lm_config.mode}" |
| 200 | ) |
| 201 | elif lm_config.provider == 'llama': |
| 202 | response = llm_llama(prompt, lm_config) |
| 203 | else: |
| 204 | raise NotImplementedError( |
| 205 | f"Provider {lm_config.provider} not implemented" |
| 206 | ) |
| 207 | |
| 208 | print(response) |