Skip to content

Commit

Permalink
Fix unmarshal to wrong variable
Browse files Browse the repository at this point in the history
  • Loading branch information
lwileczek committed May 5, 2024
1 parent 915903c commit fa88fc0
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
44 changes: 44 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Go test binaries
*.test
*.exe
*.pyc
runme.sh

# Environment stuff
env
venv
.env
.venv

# OSX trash
.DS_Store

# Developers can store local stuff in dirs named __something
__*

# Files generated by JetBrains IDEs, e.g. IntelliJ IDEA
.idea/
*.iml

# Vscode files
.vscode

# This is where the result of the go build goes
/output*/
/_output*/
/_output

# Emacs save files
*~
\#*\#
.\#*

# Vim-related files
[._]*.s[a-w][a-z]
[._]s[a-w][a-z]
*.un~
Session.vim
.netrwhist



5 changes: 4 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func (c *Client) QASearch(q string, params ...TavilyRequest) (string, error) {
ctx := context.Background()
r := c.defaultReq(q)
r.IncludeAnswer = true
r.SearchDepth = "advanced"

for _, cfg := range params {
r.SearchDepth = cfg.SearchDepth
Expand Down Expand Up @@ -145,6 +146,7 @@ func (c *Client) QASearch(q string, params ...TavilyRequest) (string, error) {
func (c *Client) QASearchWithCtx(ctx context.Context, q string, params ...TavilyRequest) (string, error) {
r := c.defaultReq(q)
r.IncludeAnswer = true
r.SearchDepth = "advanced"

for _, cfg := range params {
r.SearchDepth = cfg.SearchDepth
Expand Down Expand Up @@ -280,8 +282,9 @@ func (c *Client) search(ctx context.Context, r *TavilyRequest) (*TavilyResponse,
slog.Debug("Tavily: Unable to read the stream from the body", "err", err)
return nil, err
}

result := TavilyResponse{}
if err = json.Unmarshal(body, &resp); err != nil {
if err = json.Unmarshal(body, &result); err != nil {
slog.Debug("Issue unmarshalling response from server into our expected struct")
return nil, err
}
Expand Down
17 changes: 17 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Examples
View the examples to get a feel for how to use Tavily

### Usage
First, you'll have to add your API key to the code or to your environment with
```
export TAVILY_API_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
```
or on Windows
```ps1
set TAVILY_API_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
```
Make sure to use your real API key and not a bunch of `X` characters.
Then, switch into any directory and run `go run main.go`

### Options
- qna - Example question and answer response
25 changes: 25 additions & 0 deletions examples/qna/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"fmt"
"os"

"github.com/lwileczek/tavily"
)

func main() {
API_KEY := os.Getenv("TAVILY_API_KEY")
client, err := tavily.NewClient(API_KEY)
if err != nil {
fmt.Println("Oops must have forgot my API key!", err)
return
}

answer, err := client.QASearch("Where does Messi play right now?")
if err != nil {
fmt.Println("Unable to get an answer for that, sorry", err)
return
}

fmt.Println(answer)
}

0 comments on commit fa88fc0

Please sign in to comment.