Documentation for the Code
This Python code is primarily focused on interfacing with various language models and using them to generate text, parse outputs, and create scripts or responses. It utilizes langchain
, openai
, pydantic
, and other libraries to achieve its goals. Below is the detailed description of the main components and functions within the code.
Classes
EnvResponse
A Pydantic model representing an environment response.
- Fields:
reasoning (str)
: Reasoning about agents' actions and alignment with their goals.p1_rate (int)
: Rating of participant 1 on a scale of 0 to 9.p2_rate (int)
: Rating of participant 2 on a scale of 0 to 9.
EnvResponsePydanticOutputParser
A parser for EnvResponse
using Pydantic.
- Methods:
__init__(self, pydantic_object: Type[BaseModel] = EnvResponse)
: Initializes the parser.parse(self, text: str) -> EnvResponse
: Parses the text to create anEnvResponse
instance, removing trailing commas.get_format_instructions(self) -> str
: Returns format instructions.
ListOfIntOutputParser
Parses output to a list of integers.
-
Parameters:
number_of_int (int | None)
: Expected number of integers.range_of_int (tuple[int, int] | None)
: Expected range of integers.
-
Methods:
get_format_instructions(self) -> str
: Returns format instructions.parse(self, output: str) -> list[int]
: Parses the output string to a list of integers._type(self) -> str
: Returns the type "list[int]".
ListOfStrOutputParser
Parses output to a list of strings.
-
Parameters:
number_of_str (int | None)
: Expected number of strings.
-
Methods:
get_format_instructions(self) -> str
: Returns format instructions.parse(self, output: str) -> list[str]
: Parses the output string to a list of strings._type(self) -> str
: Returns the type "list[str]".
StrOutputParser
Parses output to a string.
- Methods:
get_format_instructions(self) -> str
: Returns format instructions.parse(self, output: str) -> str
: Parses the output string to a string._type(self) -> str
: Returns the type "str".
ScriptOutputParser
Parses script interactions.
-
Parameters:
agent_names (list[str])
: Names of the agents in the conversation.background (str)
: Background of the conversation.single_turn (bool)
: Whether the output is a single turn.
-
Methods:
get_format_instructions(self) -> str
: Returns format instructions.parse(self, output: str) -> ScriptInteractionReturnType
: Parses the output string to create aScriptInteractionReturnType
.
Functions
_return_fixed_model_version(model_name: str) -> str
Returns a fixed version of the model name.
obtain_chain(model_name: str, template: str, input_variables: list[str], temperature: float = 0.7, max_retries: int = 6) -> RunnableSerializable[dict[Any, Any], BaseMessage]
Obtains a chain for generating responses using langchain.
-
Parameters:
model_name (str)
: The model name.template (str)
: The prompt template.input_variables (list[str])
: List of input variables.temperature (float)
: Sampling temperature.max_retries (int)
: Maximum number of retries.
-
Returns:
- A
RunnableSerializable
chain.
- A
format_bad_output_for_script(ill_formed_output: str, format_instructions: str, agents: list[str], model_name: str = "gpt-4o-mini") -> BaseMessage
Formats badly structured output for a script into a proper format.
-
Parameters:
ill_formed_output (str)
: The incorrect output.format_instructions (str)
: Instructions for the correct format.agents (list[str])
: List of agent names.model_name (str)
: Model name.
-
Returns:
- A properly formatted
BaseMessage
.
- A properly formatted
agenerate(model_name: str, template: str, input_values: dict[str, str], output_parser: BaseOutputParser[OutputType], temperature: float = 0.7, structured_output: bool = False) -> OutputType
Generates asynchronous responses using langchain.
-
Parameters:
model_name (str)
: Model name.template (str)
: The template string.input_values (dict[str, str])
: Input values for the template.output_parser (BaseOutputParser[OutputType])
: Parser for the output.temperature (float)
: Sampling temperature.structured_output (bool)
: Whether to expect structured output.
-
Returns:
- The parsed output.
process_history(script: ScriptBackground | EnvResponse | dict[str, AgentAction]) -> str
Formats the script background into a readable string.
-
Parameters:
script (ScriptBackground | EnvResponse | dict[str, AgentAction])
: The script background.
-
Returns:
- A formatted string.
Usage Examples
Example to Generate Environment Profile
result = await agenerate_env_profile(
model_name="gpt-4",
inspiration_prompt="asking my boyfriend to stop being friends with his ex",
examples="Example scenario text",
temperature=0.7,
)
print(result)
Example to Parse List of Integers
parser = ListOfIntOutputParser(number_of_int=3)
output = parser.parse("1 2 3")
print(output) # Output: [1, 2, 3]
Example to Format Bad Script Output
formatted_message = format_bad_output_for_script(
ill_formed_output="Turn 1\nAgent1 said: hello.",
format_instructions="Your format instructions here.",
agents=["Agent1", "Agent2"],
)
print(formatted_message)
Example to Generate Initial Profile
profile = await agenerate_init_profile(
model_name="gpt-4",
basic_info={
"name": "Alex",
"age": "30",
"gender_identity": "male",
"pronoun": "he/him",
"occupation": "engineer",
"Big_Five_Personality": "openness, conscientiousness",
"Moral_Foundation": "care",
"Schwartz_Portrait_Value": "achievement",
"Decision_making_Style": "analytical",
"secret": "afraid of heights",
}
)
print(profile)
This code leverages advanced language models to generate structured outputs, parse different types of responses, and ensure that the generated responses adhere to specific formatting requirements. Each class and function is meticulously designed to handle specific aspects of output generation and parsing.