In order to use dento
you need to have a ChatGPT
-account.
Get Api-Key and OrgId for ChatGpt and Set them as environment-variables
export OPEN_AI_API_KEY=<Your-Api-Key>
export OPEN_AI_API_ORG=<Your-Org-Id>
dento explain
will try to explain the given keyword in the context of kubernetes using gpt-3.5-turbo
model.
To create a test mocking the communication with ChatGPT using Mockery, you can follow these steps:
-
Install the Mockery library by running
go get github.com/vektra/mockery/v2/...
in your terminal. -
Create a new file called
mock_client.go
in the same directory as yourexplainCmd
file. -
In
mock_client.go
, create an interface calledMockClient
with the same method signatures as thegoopenai.Client
interface:
type MockClient interface {
CreateCompletions(ctx context.Context, req goopenai.CreateCompletionsRequest) (*goopenai.CreateCompletionsResponse, error)
}
- Use Mockery to generate a mock implementation of
MockClient
:
mockery --name=MockClient
This will create a mock_client.go
file in a mocks
directory. This file will contain a mock implementation of the MockClient
interface.
- In your test file, import the
mocks
package:
import "your_project_path/mocks"
-
Create a new test case for your
explainCmd
function. -
Create an instance of your mock client:
mockClient := &mocks.MockClient{}
- Set up the expectations for the
CreateCompletions
method:
expectedRequest := goopenai.CreateCompletionsRequest{
Model: "gpt-3.5-turbo",
Messages: []goopenai.Message{},
Temperature: 0.2,
}
mockClient.On("CreateCompletions", context.Background(), expectedRequest).Return(
&goopenai.CreateCompletionsResponse{
Choices: []goopenai.CompletionChoice{
{
Message: goopenai.Message{
Content: "your_json_response",
},
},
},
},
nil,
)
- Inject the mock client into your
explainCmd
function:
client = mockClient
- Call your
explainCmd
function with your mock client and assert the output.