Setting the file. One moment. List · Pinecone Assistant · pinecone-io/skills · Skills Docs(opens in a new tab)
scripts/list.py
Python·208 lines·8 KB
18
19Output:
20 Formatted table or JSON list of assistants with name, status, and host
21 Optionally include files for each assistant with --files flag
22"""
23
24import os
25import sys
26import json
27import typer
28from rich.console import Console
29from rich.table import Table
30from rich.panel import Panel
31from pinecone import Pinecone
32
33app = typer.Typer()
34console = Console()
35
36
37@app.command()
38def main(
39 json_output: bool = typer.Option(False, "--json", help="Output in JSON format"),
40 files: bool = typer.Option(False, "--files", "-f", help="Include file listing for each assistant"),
41):
42 """List all Pinecone Assistants in your account."""
43
44 # Check for API key
45 api_key = os.environ.get('PINECONE_API_KEY')
46 if not api_key:
47 console.print("[red]Error: PINECONE_API_KEY environment variable not set[/red]")
48 console.print("\nGet your API key from: https://app.pinecone.io/?sessionType=signup")
49 raise typer.Exit(1)
50
51 try:
52 # Initialize Pinecone client
53 pc = Pinecone(api_key=api_key, source_tag="pinecone_skills:assistant")
54
55 # List assistants
56 assistants = pc.assistant.list_assistants()
57
58 if not assistants:
59 if json_output:
60 print(json.dumps({"assistants": [], "count": 0}))
61 else:
62 console.print("[yellow]No assistants found.[/yellow]\n")
63 console.print("Create your first assistant with:")
64 console.print(" <<next_create_first>>")
65 return
66
67 if json_output:
68 # JSON output
69 assistants_data = []
70 for asst in assistants:
71 asst_data = {
72 "name": asst.name,
73 "status": asst.status,
74 "host": getattr(asst, 'host', ''),
75 }
76
77 if files:
78 # Get files for this assistant
79 try:
80 # Models from list_assistants() carry a client back-reference,
81 # so list_files() works on them directly and returns a list.
82 # pc.assistants.list_files() returns a Paginator with no __len__.
83 file_list = asst.list_files()
84 asst_data["files"] = [
85 {
86 "name": f.name,
87 "id": f.id,
88 "status": f.status,
89 "metadata": getattr(f, 'metadata', {}),
90 }
91 for f in file_list
92 ]
93 asst_data["file_count"] = len(file_list)
94 except Exception as e:
95 asst_data["files"] = []
96 asst_data["file_count"] = 0
97 asst_data["file_error"] = str(e)
98
99 assistants_data.append(asst_data)
100
101 result = {
102 "assistants": assistants_data,
103 "count": len(assistants)
104 }
105 print(json.dumps(result, indent=2))
106 else:
107 # Rich table output
108 console.print(f"\n[bold]Found {len(assistants)} assistant(s):[/bold]\n")
109
110 # Assistants table
111 table = Table(show_header=True, header_style="bold cyan")
112 table.add_column("Name", style="green", width=30)
113 table.add_column("Status", style="yellow", width=15)
114 if files:
115 table.add_column("Files", style="magenta", width=10)
116 table.add_column("Host", style="dim", width=40 if files else 50)
117
118 for asst in assistants:
119 name = asst.name
120 status = asst.status
121 host = getattr(asst, 'host', '')
122
123 # Color code status. The API returns capitalized values
124 # ("Ready", "Initializing"), so compare case-insensitively.
125 status_key = (status or '').lower()
126 if status_key == 'ready':
127 status_display = f"[green]{status}[/green]"
128 elif status_key in ('indexing', 'initializing'):
129 status_display = f"[yellow]{status}[/yellow]"
130 else:
131 status_display = status
132
133 if files:
134 # Get file count for this assistant
135 try:
136 # Models from list_assistants() carry a client back-reference,
137 # so list_files() works on them directly and returns a list.
138 # pc.assistants.list_files() returns a Paginator with no __len__.
139 file_list = asst.list_files()
140 file_count = str(len(file_list))
141 except Exception:
142 file_count = "?"
143
144 table.add_row(name, status_display, file_count, host)
145 else:
146 table.add_row(name, status_display, host)
147
148 console.print(table)
149 console.print()
150
151 # If --files flag is set, show detailed file listing for each assistant
152 if files:
153 console.print("[bold]File Details:[/bold]\n")
154 for asst in assistants:
155 try:
156 # Models from list_assistants() carry a client back-reference,
157 # so list_files() works on them directly and returns a list.
158 # pc.assistants.list_files() returns a Paginator with no __len__.
159 file_list = asst.list_files()
160
161 if file_list:
162 # Create a table for this assistant's files
163 file_table = Table(show_header=True, header_style="bold blue", title=f"[cyan]{asst.name}[/cyan]")
164 file_table.add_column("#", style="dim", width=4)
165 file_table.add_column("File Name", style="green", width=50)
166 file_table.add_column("Status", style="yellow", width=15)
167 file_table.add_column("ID", style="dim", width=30)
168
169 for idx, file_obj in enumerate(file_list, 1):
170 file_name = file_obj.name
171 file_id = file_obj.id
172 file_status = file_obj.status
173
174 # Color code file status. The API returns
175 # capitalized values ("Available", "Processing",
176 # "ProcessingFailed"), so normalize before comparing.
177 fs_key = (file_status or '').lower()
178 if fs_key == 'available':
179 file_status_display = f"[green]{file_status}[/green]"
180 elif fs_key == 'processing':
181 file_status_display = f"[yellow]{file_status}[/yellow]"
182 elif 'failed' in fs_key:
183 file_status_display = f"[red]{file_status}[/red]"
184 else:
185 file_status_display = file_status
186
187 file_table.add_row(str(idx), file_name, file_status_display, file_id)
188
189 console.print(file_table)
190 console.print()
191 else:
192 console.print(f"[dim]{asst.name}: No files uploaded[/dim]\n")
193 except Exception as e:
194 console.print(f"[red]Error listing files for {asst.name}: {e}[/red]\n")
195
196 # Next steps panel
197 next_steps = """[bold]Next steps:[/bold]
198<<next_commands>>"""
199
200 console.print(Panel(next_steps, title="Available Commands", border_style="blue"))
201
202 except Exception as e:
203 console.print(f"[red]Error listing assistants: {e}[/red]")
204 raise typer.Exit(1)
205
206
207if __name__ == "__main__":
208 app()