Skip to content

Commit

Permalink
🎨 Improve structure / format of the code: add NoSuchOllamaHostError
Browse files Browse the repository at this point in the history
  • Loading branch information
k33g committed Feb 8, 2025
1 parent 615bc82 commit 20f3622
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 8 deletions.
19 changes: 19 additions & 0 deletions LAST_RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,25 @@ if err != nil {
```
> See these examples: `04-chat-stream` and `66-structured-outputs`
#### NoSuchOllamaHostError

```golang
// package completion
type NoSuchOllamaHostError struct {
Host string
Message string
}
```

**Usage**:
```golang
if noHostErr, ok := err.(*completion.NoSuchOllamaHostError); ok {
fmt.Printf("🦙 Got No Such Ollama Host error: %s\n", noHostErr.Message)
fmt.Printf("🌍 Expected Host: %s\n", noHostErr.Host)
}
```


### MCP


Expand Down
8 changes: 7 additions & 1 deletion completion/chat-completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/parakeet-nest/parakeet/llm"
)


func Chat(url string, query llm.Query) (llm.Answer, error) {
kindOfCompletion := "chat"

Expand Down Expand Up @@ -54,6 +53,9 @@ func Chat(url string, query llm.Query) (llm.Answer, error) {
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
if strings.HasSuffix(err.Error(), "no such host") {
return llm.Answer{}, &NoSuchOllamaHostError{Host: url, Message: "no such host"}
}
return llm.Answer{}, err
}
defer resp.Body.Close()
Expand Down Expand Up @@ -134,6 +136,10 @@ func ChatStream(url string, query llm.Query, onChunk func(llm.Answer) error) (ll
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
if strings.HasSuffix(err.Error(), "no such host") {
return llm.Answer{}, &NoSuchOllamaHostError{Host: url, Message: "no such host"}
}

return llm.Answer{}, err
}

Expand Down
11 changes: 11 additions & 0 deletions completion/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ func (e *ModelNotFoundError) Error() string {
return fmt.Sprintf("Code: %d, Message: %s, Model: %s", e.Code, e.Message, e.Model)
}

type NoSuchOllamaHostError struct {
Host string
Message string
}

func (e *NoSuchOllamaHostError) Error() string {
return fmt.Sprintf("Host: %s, Message: %s", e.Host, e.Message)
}



type CompletionError struct {
Error string
}
8 changes: 8 additions & 0 deletions completion/generate-completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func Generate(url string, query llm.GenQuery) (llm.GenAnswer, error) {
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
if strings.HasSuffix(err.Error(), "no such host") {
return llm.GenAnswer{}, &NoSuchOllamaHostError{Host: url, Message: "no such host"}
}

return llm.GenAnswer{}, err
}
defer resp.Body.Close()
Expand Down Expand Up @@ -119,6 +123,10 @@ func GenerateStream(url string, query llm.GenQuery, onChunk func(llm.GenAnswer)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
if strings.HasSuffix(err.Error(), "no such host") {
return llm.GenAnswer{}, &NoSuchOllamaHostError{Host: url, Message: "no such host"}
}

return llm.GenAnswer{}, err
}

Expand Down
1 change: 0 additions & 1 deletion examples/01-generate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ func main() {
model := "tinydolphin"



// Define the options
//options := llm.DefaultOptions()
//options.Temperature = 0.5
Expand Down
10 changes: 8 additions & 2 deletions examples/04-chat-stream/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,14 @@ func main() {
fmt.Printf("💥 Got Model Not Found error: %s\n", modelErr.Message)
fmt.Printf("😡 Error code: %d\n", modelErr.Code)
fmt.Printf("🧠 Expected Model: %s\n", modelErr.Model)
} else {
log.Fatal("😡:", err)
}

if noHostErr, ok := err.(*completion.NoSuchOllamaHostError); ok {
fmt.Printf("🦙 Got No Such Ollama Host error: %s\n", noHostErr.Message)
fmt.Printf("🌍 Expected Host: %s\n", noHostErr.Host)
}

log.Fatal("😡:", err)

}
}
4 changes: 2 additions & 2 deletions examples/66-structured-outputs/.env
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# if working from a container
OLLAMA_HOST=http://host.docker.internal:11434
#OLLAMA_HOST=http://localhost:11434
#OLLAMA_HOST=http://host.docker.internal:11434
OLLAMA_HOST=http://localhost:11434
LLM_CHAT=qwen2.5:1.5b

8 changes: 6 additions & 2 deletions examples/66-structured-outputs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,13 @@ func main() {
fmt.Printf("💥 Got Model Not Found error: %s\n", modelErr.Message)
fmt.Printf("😡 Error code: %d\n", modelErr.Code)
fmt.Printf("🧠 Expected Model: %s\n", modelErr.Model)
} else {
log.Fatal("😡:", err)
}
if noHostErr, ok := err.(*completion.NoSuchOllamaHostError); ok {
fmt.Printf("🦙 Got No Such Ollama Host error: %s\n", noHostErr.Message)
fmt.Printf("🌍 Expected Host: %s\n", noHostErr.Host)
}
log.Fatal("😡:", err)

}
fmt.Println(answer.Message.Content)

Expand Down

0 comments on commit 20f3622

Please sign in to comment.