|
1 |
| -import os |
2 |
| -import sys |
3 |
| -import logging |
4 |
| -import asyncio |
5 |
| -from dotenv import load_dotenv |
6 |
| -from openai import AsyncOpenAI |
7 |
| -from openai.types.beta.assistant_create_params import AssistantCreateParams |
8 |
| -from openai.types.beta.assistant_tool_param import CodeInterpreterToolParam, FileSearchToolParam, FunctionToolParam |
9 |
| -from openai.types.beta.assistant import Assistant |
10 |
| -from openai.types import FunctionDefinition |
11 |
| - |
12 |
| -# Configure logger to stream to console |
13 |
| -logging.basicConfig(level=logging.INFO, stream=sys.stdout) |
14 |
| -logger: logging.Logger = logging.getLogger(__name__) |
15 |
| - |
16 |
| - |
17 |
| -request: AssistantCreateParams = AssistantCreateParams( |
18 |
| - instructions="You are a helpful assistant.", |
19 |
| - name="Quickstart Assistant", |
20 |
| - model="gpt-4o", |
21 |
| - tools=[ |
22 |
| - CodeInterpreterToolParam(type="code_interpreter"), |
23 |
| - FileSearchToolParam(type="file_search"), |
24 |
| - FunctionToolParam( |
25 |
| - type="function", |
26 |
| - function=FunctionDefinition( |
27 |
| - name="get_weather", |
28 |
| - description="Determine weather in my location", |
29 |
| - parameters={ |
30 |
| - "type": "object", |
31 |
| - "properties": { |
32 |
| - "location": { |
33 |
| - "type": "string", |
34 |
| - "description": "The city and state e.g. San Francisco, CA" |
35 |
| - } |
36 |
| - }, |
37 |
| - "required": ["location"], |
38 |
| - "additionalProperties": False, |
39 |
| - }, |
40 |
| - strict=True, |
41 |
| - ) |
42 |
| - ), |
43 |
| - ], |
44 |
| -) |
45 |
| - |
46 |
| - |
47 |
| -def update_env_file(assistant_id, logger): |
48 |
| - """ |
49 |
| - Update the .env file with a new assistant ID. |
50 |
| -
|
51 |
| - If the .env file already contains an ASSISTANT_ID, it will be removed. |
52 |
| - The new assistant ID will be appended to the .env file. If the .env file |
53 |
| - does not exist, it will be created. |
54 |
| - """ |
55 |
| - if os.path.exists('.env'): |
56 |
| - with open('.env', 'r') as env_file: |
57 |
| - lines = env_file.readlines() |
58 |
| - |
59 |
| - # Remove any existing ASSISTANT_ID line |
60 |
| - lines = [line for line in lines if not line.startswith("ASSISTANT_ID=")] |
61 |
| - |
62 |
| - # Write back the modified lines |
63 |
| - with open('.env', 'w') as env_file: |
64 |
| - env_file.writelines(lines) |
65 |
| - |
66 |
| - # Write the new assistant ID to the .env file |
67 |
| - with open('.env', 'a') as env_file: |
68 |
| - env_file.write(f"ASSISTANT_ID={assistant_id}\n") |
69 |
| - logger.info(f"Assistant ID written to .env: {assistant_id}") |
70 |
| - |
71 |
| - |
72 |
| -async def create_or_update_assistant( |
73 |
| - client: AsyncOpenAI, |
74 |
| - assistant_id: str, |
75 |
| - request: AssistantCreateParams, |
76 |
| - logger: logging.Logger |
77 |
| -): |
78 |
| - """ |
79 |
| - Create or update the assistant based on the presence of an assistant_id. |
80 |
| - """ |
81 |
| - try: |
82 |
| - if assistant_id: |
83 |
| - # Update the existing assistant |
84 |
| - assistant: Assistant = await client.beta.assistants.update( |
85 |
| - assistant_id, |
86 |
| - **request |
87 |
| - ) |
88 |
| - logger.info(f"Updated assistant with ID: {assistant_id}") |
89 |
| - else: |
90 |
| - # Create a new assistant |
91 |
| - assistant: Assistant = await client.beta.assistants.create(**request) |
92 |
| - logger.info(f"Created new assistant: {assistant}") |
93 |
| - |
94 |
| - # Update the .env file with the new assistant ID |
95 |
| - update_env_file(assistant.id, logger) |
96 |
| - |
97 |
| - except Exception as e: |
98 |
| - action = "update" if assistant_id else "create" |
99 |
| - logger.error(f"Failed to {action} assistant: {e}") |
100 |
| - |
101 |
| - |
102 |
| -# Run the assistant creation in an asyncio event loop |
103 |
| -if __name__ == "__main__": |
104 |
| - load_dotenv() |
105 |
| - assistant_id = os.getenv("ASSISTANT_ID") |
106 |
| - |
107 |
| - # Initialize the OpenAI client |
108 |
| - openai: AsyncOpenAI = AsyncOpenAI() |
109 |
| - |
110 |
| - # Run the main function in an asyncio event loop |
111 |
| - new_assistant_id: Assistant = asyncio.run( |
112 |
| - create_or_update_assistant(openai, assistant_id, request, logger) |
113 |
| - ) |
114 |
| - logger.info(f"Assistant created with ID: {new_assistant_id}") |
| 1 | +import os |
| 2 | +import logging |
| 3 | +import asyncio |
| 4 | +from dotenv import load_dotenv |
| 5 | +from openai import AsyncOpenAI |
| 6 | +from openai.types.beta.assistant_create_params import AssistantCreateParams |
| 7 | +from openai.types.beta.assistant_tool_param import CodeInterpreterToolParam, FileSearchToolParam, FunctionToolParam |
| 8 | +from openai.types.beta.assistant import Assistant |
| 9 | +from openai.types import FunctionDefinition |
| 10 | + |
| 11 | + |
| 12 | +request: AssistantCreateParams = AssistantCreateParams( |
| 13 | + instructions="You are a helpful assistant.", |
| 14 | + name="Quickstart Assistant", |
| 15 | + model="gpt-4o", |
| 16 | + tools=[ |
| 17 | + CodeInterpreterToolParam(type="code_interpreter"), |
| 18 | + FileSearchToolParam(type="file_search"), |
| 19 | + FunctionToolParam( |
| 20 | + type="function", |
| 21 | + function=FunctionDefinition( |
| 22 | + name="get_weather", |
| 23 | + description="Determine weather in my location", |
| 24 | + parameters={ |
| 25 | + "type": "object", |
| 26 | + "properties": { |
| 27 | + "location": { |
| 28 | + "type": "string", |
| 29 | + "description": "The city and state e.g. San Francisco, CA" |
| 30 | + } |
| 31 | + }, |
| 32 | + "required": ["location"], |
| 33 | + "additionalProperties": False, |
| 34 | + }, |
| 35 | + strict=True, |
| 36 | + ) |
| 37 | + ), |
| 38 | + ], |
| 39 | +) |
| 40 | + |
| 41 | + |
| 42 | +def update_env_file(var_name: str, var_value: str, logger: logging.Logger): |
| 43 | + """ |
| 44 | + Update the .env file with a new environment variable. |
| 45 | +
|
| 46 | + If the .env file already contains the specified variable, it will be updated. |
| 47 | + The new value will be appended to the .env file if it doesn't exist. |
| 48 | + If the .env file does not exist, it will be created. |
| 49 | +
|
| 50 | + Args: |
| 51 | + var_name: The name of the environment variable to update |
| 52 | + var_value: The value to set for the environment variable |
| 53 | + logger: Logger instance for output |
| 54 | + """ |
| 55 | + if os.path.exists('.env'): |
| 56 | + with open('.env', 'r') as env_file: |
| 57 | + lines = env_file.readlines() |
| 58 | + |
| 59 | + # Remove any existing line with this variable |
| 60 | + lines = [line for line in lines if not line.startswith(f"{var_name}=")] |
| 61 | + |
| 62 | + # Write back the modified lines |
| 63 | + with open('.env', 'w') as env_file: |
| 64 | + env_file.writelines(lines) |
| 65 | + |
| 66 | + # Write the new variable to the .env file |
| 67 | + with open('.env', 'a') as env_file: |
| 68 | + env_file.write(f"{var_name}={var_value}\n") |
| 69 | + logger.debug(f"Environment variable {var_name} written to .env: {var_value}") |
| 70 | + |
| 71 | + |
| 72 | +async def create_or_update_assistant( |
| 73 | + client: AsyncOpenAI, |
| 74 | + assistant_id: str, |
| 75 | + request: AssistantCreateParams, |
| 76 | + logger: logging.Logger |
| 77 | +) -> str: |
| 78 | + """ |
| 79 | + Create or update the assistant based on the presence of an assistant_id. |
| 80 | + """ |
| 81 | + try: |
| 82 | + if assistant_id: |
| 83 | + # Update the existing assistant |
| 84 | + assistant: Assistant = await client.beta.assistants.update( |
| 85 | + assistant_id, |
| 86 | + **request |
| 87 | + ) |
| 88 | + logger.info(f"Updated assistant with ID: {assistant_id}") |
| 89 | + else: |
| 90 | + # Create a new assistant |
| 91 | + assistant: Assistant = await client.beta.assistants.create(**request) |
| 92 | + logger.info(f"Created new assistant: {assistant.id}") |
| 93 | + |
| 94 | + # Update the .env file with the new assistant ID |
| 95 | + update_env_file("ASSISTANT_ID", assistant.id, logger) |
| 96 | + |
| 97 | + return assistant.id |
| 98 | + |
| 99 | + except Exception as e: |
| 100 | + action = "update" if assistant_id else "create" |
| 101 | + logger.error(f"Failed to {action} assistant: {e}") |
| 102 | + |
| 103 | + |
| 104 | +# Run the assistant creation in an asyncio event loop |
| 105 | +if __name__ == "__main__": |
| 106 | + import sys |
| 107 | + |
| 108 | + # Configure logger to stream to console |
| 109 | + logging.basicConfig(level=logging.INFO, stream=sys.stdout) |
| 110 | + logger: logging.Logger = logging.getLogger(__name__) |
| 111 | + |
| 112 | + load_dotenv() |
| 113 | + assistant_id = os.getenv("ASSISTANT_ID") |
| 114 | + |
| 115 | + # Initialize the OpenAI client |
| 116 | + openai: AsyncOpenAI = AsyncOpenAI() |
| 117 | + |
| 118 | + # Run the main function in an asyncio event loop |
| 119 | + asyncio.run( |
| 120 | + create_or_update_assistant(openai, assistant_id, request, logger) |
| 121 | + ) |
0 commit comments