feat: implement ListModels function to retrieve model IDs from OpenAI
Need testing with OpenAI api key.
This should work without any problems.
Error message:
Edit: I don't have an openai key. If anyone can test the command I'll be grateful
@tuannvm can you help @Azathoth-X test this out ?
@hakman might also help here with the test.
I can have a look today
@Azathoth-X
>>> models
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x18 pc=0x101423a30]
goroutine 1 [running]:
github.com/openai/openai-go/internal/requestconfig.(*RequestConfig).Apply(...)
/Users/tuannvm/golang/pkg/mod/github.com/openai/[email protected]/internal/requestconfig/requestconfig.go:586
github.com/openai/openai-go/internal/requestconfig.NewRequestConfig({0x10282b1d0, 0x1031c7860}, {0x101975673, 0x3}, {0x101979a52, 0x6}, {0x0, 0x0}, {0x102579e00, 0x1400008c070}, ...)
/Users/tuannvm/golang/pkg/mod/github.com/openai/[email protected]/internal/requestconfig/requestconfig.go:173 +0xdf0
github.com/openai/openai-go.(*ModelService).List(0x140002cc140, {0x10282b1d0, 0x1031c7860}, {0x140005cf5e8, 0x1, 0x1f800bf00c0?})
/Users/tuannvm/golang/pkg/mod/github.com/openai/[email protected]/model.go:58 +0x1e0
github.com/GoogleCloudPlatform/kubectl-ai/gollm.(*OpenAIClient).ListModels(0x140002cc008, {0x10282b1d0, 0x1031c7860})
/Users/tuannvm/Projects/cli/google-kubectl-ai/gollm/openai.go:165 +0x8c
main.(*session).listModels(0x140005cf928, {0x10282b1d0?, 0x1031c7860?})
/Users/tuannvm/Projects/cli/google-kubectl-ai/main.go:476 +0x3c
main.(*session).answerQuery(0x140005cf928, {0x10282b1d0?, 0x1031c7860?}, {0x14000125e80?, 0x1400049fd40?})
/Users/tuannvm/Projects/cli/google-kubectl-ai/main.go:498 +0x254
main.(*session).repl(0x140005cf928, {0x10282b1d0, 0x1031c7860}, {0x0?, 0x0?})
/Users/tuannvm/Projects/cli/google-kubectl-ai/main.go:463 +0x1c8
main.RunRootCommand({0x10282b1d0, 0x1031c7860}, {{0x16f0c2a4c, 0x6}, {0x1019ac234, 0x1c}, 0x0, 0x0, 0x0, 0x0, ...}, ...)
/Users/tuannvm/Projects/cli/google-kubectl-ai/main.go:413 +0x81c
main.BuildRootCommand.func1(0x1400048e100?, {0x14000446120?, 0x4?, 0x101976096?})
/Users/tuannvm/Projects/cli/google-kubectl-ai/main.go:62 +0x98
github.com/spf13/cobra.(*Command).execute(0x1400021e008, {0x140001181c0, 0x2, 0x2})
/Users/tuannvm/golang/pkg/mod/github.com/spf13/[email protected]/command.go:1015 +0x828
github.com/spf13/cobra.(*Command).ExecuteC(0x1400021e008)
/Users/tuannvm/golang/pkg/mod/github.com/spf13/[email protected]/command.go:1148 +0x350
github.com/spf13/cobra.(*Command).Execute(...)
/Users/tuannvm/golang/pkg/mod/github.com/spf13/[email protected]/command.go:1071
github.com/spf13/cobra.(*Command).ExecuteContext(...)
/Users/tuannvm/golang/pkg/mod/github.com/spf13/[email protected]/command.go:1064
main.run({0x10282b1d0, 0x1031c7860})
/Users/tuannvm/Projects/cli/google-kubectl-ai/main.go:249 +0x3cc
main.main()
/Users/tuannvm/Projects/cli/google-kubectl-ai/main.go:208 +0xb8
Applied targeted fixes to avoid passing a nil RequestOption to the OpenAI client. Now ListModels builds a slice of options and passes it variadically:
• Changed var opt option.RequestOption = nil → var opts []option.RequestOption
• Updated the endpoint block to append into opts instead of setting opt
• Switched the call from List(ctx, opt) to List(ctx, opts...)
These changes prevent a nil pointer deref in internal/requestconfig when no endpoint is set.
@tuannvm Thank you for the learnings, my concepts aren't as clear as I thought. I would once again request you to test the changes.
Thanks @tuannvm for helping @Azathoth-X .
I will defer to @tuannvm for final review, once he approves it, I can get this in.