summaryrefslogtreecommitdiff
path: root/cli.py
blob: e858f9eebedb80c7ef795d83592ca8a5b5bc8a3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python
# COPYRIGHT 2025 Thomas Grothe
import argparse
import os
import sys
import ai_api_module

#this is the CLI program used to interact with the GAI python module. 

default_model=None

def main():
    """Main function to parse arguments and execute commands"""
    parser = argparse.ArgumentParser(description="CLI for calling AI APIs with file attachment support")
    parser.add_argument(
        "--apikey", default=os.getenv("GEMINI_API_KEY"), help="API key for the service"
    )
    parser.add_argument("--modelid", default=default_model, help=f"ID of the model to use (default={default_model}")
    parser.add_argument("--list-models", action="store_true", help="list the available models")
    parser.add_argument("--assistantid", default="default_chatbot", help="ID of the assistant")
    parser.add_argument("--list-assistants", "--list-a", "--list-assistant", action='store_true', help='list all the available assistants')
    parser.add_argument('--list-conversations', '--list-conversation', '--list-convo', '--list-c', action='store_true', help='list previous conversations')
    parser.add_argument("--query", "-q", default="", help="Query to be sent to the assistant")
    parser.add_argument("--topp", type=float, default=0.9, help="Top P value for the model")
    parser.add_argument(
        "--temperature", type=float, default=0.7, help="Temperature value for the model"
    )
    parser.add_argument("--stream", action="store_true", help="Enable streaming response")
    parser.add_argument("--no-stream", action="store_true", help="Disable streaming response")
    parser.add_argument("--output_mode", "-o", nargs=1, action="store", default='text', help="just output the json") 
    
    #parser.add_argument("--file-health", action="store_true", help="")
    #parser.add_argument("--file-list", action="store_true", help="List all uploaded files")
    #parser.add_argument("--file-upload", help="Upload a file to the service")
    #parser.add_argument("--file-status", help="Check status of a file by ID")
    #parser.add_argument("--file-purpose", default="assistants", help="Purpose of the file")
    #parser.add_argument("--attach-files", nargs="*", help="File IDs to attach to the query")
    
    parser.add_argument("--new", "-n", help="force new session and new chat", action="store_true", default=False) #TODO: solidify a good approach here

    parser.add_argument("extra_query", nargs="*")

    args = parser.parse_args()
    
    # Try to get API key if not provided
    if args.apikey:
        ai_api_module.api_key = args.apikey
    else:
        ai_api_module.get_api_key()
        if not ai_api_module.api_key:
            print("Error: API key not provided")
            sys.exit(1)
    
    if args.list_models:
        res = ai_api_module.list_models(args.output_mode)
        if res[0]:
          for m in res[1]:
            print(str(m))
            #print(f"  ModelID: {m['id']}\n    Description: {m['description']}\n    Tokens: {m['max_completion_tokens']}\n    Modalities: {str(m['modalities'])}")
            print()
        else:
          print('error: ' + str(res[1]))
        return

    if args.list_conversations:
      res = ai_api_module.list_conversations(args.output_mode)
      print(res)
      return

    if args.list_assistants:
        res = ai_api_module.list_assistants(args.output_mode)
        if res[0]:
          for a in res[1]:
            print(f"    ID: {a['id']}\n     Name: {a['display_name']}")
            print()
        else:
          print('error: ' + str(res[1]))
        return


    # Process query
    if args.query or args.extra_query:
        query = args.query
        if args.extra_query:
            if query:
                query += " " + " ".join(args.extra_query)
            else:
                query = " ".join(args.extra_query)
                
        response = ai_api_module.query( 
            args.modelid, 
            query,
            args.topp,
            args.temperature, 
            not args.no_stream and args.stream,
        )
        
        if response and not args.stream:
            print(response)
    else:
        parser.print_help()

if __name__ == "__main__":
    main()