Skip to content

Commit

Permalink
feat: add citations object to CompletionResponse (#20)
Browse files Browse the repository at this point in the history
* add citations object to completion response

* remove temporary empty string return in the String function

* add test cases for GetCitations
  • Loading branch information
dickeyy authored Jan 23, 2025
1 parent 2f7925d commit 3e621e8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
3 changes: 3 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ func main() {
}

fmt.Println(res.GetLastContent())
for i, c := range res.GetCitations() {
fmt.Printf("Citation %d: %s", i+1, c)
}
// fmt.Printf("%+v\n", *req)
// fmt.Println("*************")
// fmt.Printf("%+v\n", res)
Expand Down
21 changes: 15 additions & 6 deletions perplexity_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ type Choice struct {

// CompletionResponse is a response object for the Perplexity API.
type CompletionResponse struct {
ID string `json:"id"`
Model string `json:"model"`
Created int `json:"created"`
Usage Usage `json:"usage"`
Object string `json:"object"`
Choices []Choice `json:"choices"`
ID string `json:"id"`
Model string `json:"model"`
Created int `json:"created"`
Usage Usage `json:"usage"`
Object string `json:"object"`
Choices []Choice `json:"choices"`
Citations *[]string `json:"citations,omitempty"`
}

// String returns a string representation of the CompletionResponse.
Expand All @@ -52,3 +53,11 @@ func (r *CompletionResponse) GetLastContent() string {
}
return r.Choices[len(r.Choices)-1].Message.Content
}

// GetCitations returns the citations of the completion response.
func (r *CompletionResponse) GetCitations() []string {
if r.Citations == nil {
return []string{}
}
return *r.Citations
}
21 changes: 21 additions & 0 deletions perplexity_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,24 @@ func TestString(t *testing.T) {
assert.Equal(t, "{\n \"id\": \"id\",\n \"model\": \"model\",\n \"created\": 1,\n \"usage\": {\n \"prompt_tokens\": 1,\n \"completion_tokens\": 1,\n \"total_tokens\": 1\n },\n \"object\": \"object\",\n \"choices\": [\n {\n \"index\": 0,\n \"finish_reason\": \"\",\n \"message\": {\n \"role\": \"assistant\",\n \"content\": \"hello\"\n },\n \"delta\": {\n \"role\": \"\",\n \"content\": \"\"\n }\n }\n ]\n}", content.String())
})
}

func TestGetCitations(t *testing.T) {
t.Run("empty response returns empty citations", func(t *testing.T) {
content := perplexity.CompletionResponse{}
assert.Equal(t, content.GetCitations(), []string{})
})

t.Run("nil citations returns empty citations", func(t *testing.T) {
content := perplexity.CompletionResponse{
Citations: nil,
}
assert.Equal(t, content.GetCitations(), []string{})
})

t.Run("case with a real citations", func(t *testing.T) {
content := perplexity.CompletionResponse{
Citations: &[]string{"citation1", "citation2"},
}
assert.Equal(t, content.GetCitations(), []string{"citation1", "citation2"})
})
}

0 comments on commit 3e621e8

Please sign in to comment.