Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: replace togetherai call during filtering stage #148

Merged
merged 1 commit into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions composer/composer.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,36 +187,45 @@ func (c *Composer) Filter(ctx context.Context, news journalist.NewsList) (journa
return nil, newError(err, errlvl.ERROR, "Filter", "ToContentJSON").WithValue(fmt.Sprintf("%+v", news))
}

resp, err := c.TogetherAIClient.CreateChatCompletion(
resp, err := c.OpenAiClient.CreateChatCompletion(
ctx,
togetherAIRequest{
Model: "mistralai/Mixtral-8x7B-Instruct-v0.1",
Prompt: c.Config.FilterPromptInstruct(jsonNews),
MaxTokens: 2048,
Temperature: 0.7,
TopP: 0.7,
TopK: 50,
RepetitionPenalty: 1,
Stop: []string{"[/INST]", "</s>"},
openai.ChatCompletionRequest{
Model: openai.GPT4oMini,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
Content: c.Config.FilterPrompt(),
},
{
Role: openai.ChatMessageRoleUser,
Content: jsonNews,
},
},
Temperature: 0.7,
MaxTokens: 2048,
TopP: 0.7,
FrequencyPenalty: 0,
PresencePenalty: 0,
},
)

if err != nil {
return nil, newError(err, errlvl.WARN, "Filter", "TogetherAIClient.CreateChatCompletion")
return nil, newError(err, errlvl.WARN, "Filter", "OpenAiClient.CreateChatCompletion")
}

if len(resp.Choices) == 0 {
return nil, newError(errors.New("empty response"), errlvl.WARN, "Filter", "TogetherAIClient.CreateChatCompletion")
return nil, newError(errors.New("empty response"), errlvl.WARN, "Filter", "OpenAiClient.CreateChatCompletion")
}

matches, err := aiJSONStringFixer(resp.Choices[0].Text)
matches, err := aiJSONStringFixer(resp.Choices[0].Message.Content)
if err != nil {
return nil, newError(err, errlvl.ERROR, "Filter", "aiJSONStringFixer")
}

var chosenByAi journalist.NewsList
err = json.Unmarshal([]byte(matches), &chosenByAi)
if err != nil {
return nil, newError(err, errlvl.ERROR, "Filter", "json.Unmarshal").WithValue(resp.Choices[0].Text)
return nil, newError(err, errlvl.ERROR, "Filter", "json.Unmarshal").WithValue(resp.Choices[0].Message.Content)
}

// Create a map of chosenByAi news IDs to quickly find them
Expand Down
55 changes: 26 additions & 29 deletions composer/composer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,6 @@ func (m *MockOpenAiClient) CreateChatCompletion(ctx context.Context, req openai.
return args.Get(0).(openai.ChatCompletionResponse), args.Error(1) //nolint:wrapcheck
}

type MockTogetherAIClient struct {
mock.Mock
}

func (m *MockTogetherAIClient) CreateChatCompletion(ctx context.Context, options togetherAIRequest) (*TogetherAIResponse, error) {
args := m.Called(ctx, options)
return args.Get(0).(*TogetherAIResponse), args.Error(1) //nolint:wrapcheck
}

func TestComposer_Compose(t *testing.T) {
news := journalist.NewsList{
{
Expand Down Expand Up @@ -392,43 +383,49 @@ func TestComposer_Filter(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockClient := new(MockTogetherAIClient)
mockClient := new(MockOpenAiClient)
defConf := defaultPromptConfig()

// Set expectations for the mock client
if tt.wantErr {
mockError := errors.New("some error")
mockClient.On("CreateChatCompletion", mock.Anything, mock.Anything).Return(&TogetherAIResponse{}, mockError)
mockClient.On("CreateChatCompletion", mock.Anything, mock.Anything).Return(&openai.ChatCompletionResponse{}, mockError)
} else {
jsonNews, _ := tt.args.news.RemoveFlagged().ToContentJSON()
expectedJSONNews, _ := tt.want.RemoveFlagged().ToContentJSON()

mockClient.On("CreateChatCompletion",
mock.Anything,
togetherAIRequest{
Model: "mistralai/Mixtral-8x7B-Instruct-v0.1",
Prompt: defConf.FilterPromptInstruct(jsonNews),
MaxTokens: 2048,
Temperature: 0.7,
TopP: 0.7,
TopK: 50,
RepetitionPenalty: 1,
Stop: []string{"[/INST]", "</s>"},
mockClient.On("CreateChatCompletion", mock.Anything, openai.ChatCompletionRequest{
Model: openai.GPT4oMini,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
Content: defConf.FilterPrompt(),
},
{
Role: openai.ChatMessageRoleUser,
Content: jsonNews,
},
},
).Return(&TogetherAIResponse{
Choices: []struct {
Text string `json:"text"`
}{
Temperature: 0.7,
MaxTokens: 2048,
TopP: 0.7,
FrequencyPenalty: 0,
PresencePenalty: 0,
},
).Return(openai.ChatCompletionResponse{
Choices: []openai.ChatCompletionChoice{
{
Text: expectedJSONNews,
Message: openai.ChatCompletionMessage{
Content: expectedJSONNews,
},
},
},
}, nil)
}

c := &Composer{
TogetherAIClient: mockClient,
Config: defaultPromptConfig(),
OpenAiClient: mockClient,
Config: defaultPromptConfig(),
}
got, err := c.Filter(context.Background(), tt.args.news)
if (err != nil) != tt.wantErr {
Expand Down
9 changes: 9 additions & 0 deletions composer/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "fmt"
type promptConfig struct {
ComposePrompt string
SummarisePrompt summarisePromptFunc
FilterPrompt func() string
FilterPromptInstruct filterPromptFunc
}

Expand Down Expand Up @@ -39,6 +40,14 @@ func defaultPromptConfig() *promptConfig {
headlinesLimit,
)
},
FilterPrompt: func() string {
return `You will be given a JSON array of financial news.
You need to remove from array blank, purposeless, clickbait, advertising or non-financial news.
Most important news right know is inflation, interest rates, war, elections, crisis, unemployment index etc.
Always answer in the following JSON format: [{\"ID\":\"\",\"Title\":\"\",\"Description\":\"\"}] or [].
----------------------------------------
ONLY JSON IS ALLOWED as an answer. No explanation or other text is allowed.`
},
FilterPromptInstruct: func(newsJson string) string {
return fmt.Sprintf(`[INST]You will be given a JSON array of financial news.
You need to remove from array blank, purposeless, clickbait, advertising or non-financial news.
Expand Down