-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
49a9d45
commit ebe86b3
Showing
5 changed files
with
171 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package mistral | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
// FIMRequestParams represents the parameters for the FIM method of MistralClient. | ||
type FIMRequestParams struct { | ||
Model string `json:"model"` | ||
Prompt string `json:"prompt"` | ||
Suffix string `json:"suffix"` | ||
MaxTokens int `json:"max_tokens"` | ||
Temperature float64 `json:"temperature"` | ||
Stop []string `json:"stop,omitempty"` | ||
} | ||
|
||
// FIMCompletionResponse represents the response from the FIM completion endpoint. | ||
type FIMCompletionResponse struct { | ||
ID string `json:"id"` | ||
Object string `json:"object"` | ||
Created int `json:"created"` | ||
Model string `json:"model"` | ||
Choices []FIMCompletionResponseChoice `json:"choices"` | ||
Usage UsageInfo `json:"usage"` | ||
} | ||
|
||
// FIMCompletionResponseChoice represents a choice in the FIM completion response. | ||
type FIMCompletionResponseChoice struct { | ||
Index int `json:"index"` | ||
Message ChatMessage `json:"message"` | ||
FinishReason FinishReason `json:"finish_reason,omitempty"` | ||
} | ||
|
||
// FIM sends a FIM request and returns the completion response. | ||
func (c *MistralClient) FIM(params *FIMRequestParams) (*FIMCompletionResponse, error) { | ||
requestData := map[string]interface{}{ | ||
"model": params.Model, | ||
"prompt": params.Prompt, | ||
"suffix": params.Suffix, | ||
"max_tokens": params.MaxTokens, | ||
"temperature": params.Temperature, | ||
} | ||
|
||
if params.Stop != nil { | ||
requestData["stop"] = params.Stop | ||
} | ||
|
||
response, err := c.request(http.MethodPost, requestData, "v1/fim/completions", false, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
respData, ok := response.(map[string]interface{}) | ||
if !ok { | ||
return nil, fmt.Errorf("invalid response type: %T", response) | ||
} | ||
|
||
var fimResponse FIMCompletionResponse | ||
err = mapToStruct(respData, &fimResponse) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &fimResponse, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package mistral | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFIM(t *testing.T) { | ||
client := NewMistralClientDefault("") | ||
params := FIMRequestParams{ | ||
Model: ModelCodestralLatest, | ||
Prompt: "def f(", | ||
Suffix: "return a + b", | ||
MaxTokens: 64, | ||
Temperature: 0, | ||
Stop: []string{"\n"}, | ||
} | ||
res, err := client.FIM(¶ms) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, res) | ||
|
||
assert.Greater(t, len(res.Choices), 0) | ||
assert.Equal(t, res.Choices[0].Message.Content, "a, b):") | ||
assert.Equal(t, res.Choices[0].FinishReason, FinishReasonStop) | ||
} | ||
|
||
func TestFIMWithStop(t *testing.T) { | ||
client := NewMistralClientDefault("") | ||
params := FIMRequestParams{ | ||
Model: ModelCodestralLatest, | ||
Prompt: "def is_odd(n): \n return n % 2 == 1 \n def test_is_odd():", | ||
Suffix: "test_is_odd()", | ||
MaxTokens: 64, | ||
Temperature: 0, | ||
Stop: []string{"False"}, | ||
} | ||
res, err := client.FIM(¶ms) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, res) | ||
|
||
assert.Greater(t, len(res.Choices), 0) | ||
assert.Equal(t, res.Choices[0].Message.Content, "\n assert is_odd(1) == True\n assert is_odd(2) == ") | ||
assert.Equal(t, res.Choices[0].FinishReason, FinishReasonStop) | ||
} | ||
|
||
func TestFIMInvalidModel(t *testing.T) { | ||
client := NewMistralClientDefault("") | ||
params := FIMRequestParams{ | ||
Model: "invalid-model", | ||
Prompt: "This is a test prompt", | ||
Suffix: "This is a test suffix", | ||
MaxTokens: 10, | ||
Temperature: 0.5, | ||
} | ||
res, err := client.FIM(¶ms) | ||
assert.Error(t, err) | ||
assert.Nil(t, res) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters