mirror of
https://github.com/ollama/ollama.git
synced 2026-01-19 04:51:17 -05:00
Compare commits
3 Commits
parth/decr
...
nicole/web
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f944382424 | ||
|
|
3aa34ff0e6 | ||
|
|
0797490d9a |
@@ -370,6 +370,24 @@ func (c *Client) ListRunning(ctx context.Context) (*ProcessResponse, error) {
|
|||||||
return &lr, nil
|
return &lr, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WebSearch performs a web search using the ollama service.
|
||||||
|
func (c *Client) WebSearch(ctx context.Context, req *SearchRequest) (*SearchResponse, error) {
|
||||||
|
var sr SearchResponse
|
||||||
|
if err := c.do(ctx, http.MethodPost, "/api/web_search", req, &sr); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &sr, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch retrieves content from a URL using the ollama service.
|
||||||
|
func (c *Client) Fetch(ctx context.Context, req *FetchRequest) (*FetchResponse, error) {
|
||||||
|
var fr FetchResponse
|
||||||
|
if err := c.do(ctx, http.MethodPost, "/api/web_fetch", req, &fr); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &fr, nil
|
||||||
|
}
|
||||||
|
|
||||||
// Copy copies a model - creating a model with another name from an existing
|
// Copy copies a model - creating a model with another name from an existing
|
||||||
// model.
|
// model.
|
||||||
func (c *Client) Copy(ctx context.Context, req *CopyRequest) error {
|
func (c *Client) Copy(ctx context.Context, req *CopyRequest) error {
|
||||||
|
|||||||
27
api/types.go
27
api/types.go
@@ -1058,3 +1058,30 @@ func FormatParams(params map[string][]string) (map[string]any, error) {
|
|||||||
|
|
||||||
return out, nil
|
return out, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Web search types
|
||||||
|
type SearchRequest struct {
|
||||||
|
Query string `json:"query"`
|
||||||
|
MaxResults int `json:"max_results,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type SearchResult struct {
|
||||||
|
Title string `json:"title"`
|
||||||
|
URL string `json:"url"`
|
||||||
|
Content string `json:"content"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type SearchResponse struct {
|
||||||
|
Results []SearchResult `json:"results"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Web fetch types
|
||||||
|
type FetchRequest struct {
|
||||||
|
URL string `json:"url"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type FetchResponse struct {
|
||||||
|
Content string `json:"content"`
|
||||||
|
Title string `json:"title,omitempty"`
|
||||||
|
URL string `json:"url"`
|
||||||
|
}
|
||||||
|
|||||||
104
server/routes.go
104
server/routes.go
@@ -1227,6 +1227,108 @@ func (s *Server) CopyHandler(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Server) WebSearchHandler(c *gin.Context) {
|
||||||
|
var req api.SearchRequest
|
||||||
|
if err := c.ShouldBindJSON(&req); errors.Is(err, io.EOF) {
|
||||||
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "missing request body"})
|
||||||
|
return
|
||||||
|
} else if err != nil {
|
||||||
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.Query == "" {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "query is required"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.MaxResults <= 0 {
|
||||||
|
req.MaxResults = 5
|
||||||
|
}
|
||||||
|
|
||||||
|
results, err := s.callWebSearchAPI(req.Query, req.MaxResults)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(results) > req.MaxResults {
|
||||||
|
results = results[:req.MaxResults]
|
||||||
|
}
|
||||||
|
|
||||||
|
resp := api.SearchResponse{
|
||||||
|
Results: results,
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, resp)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) callWebSearchAPI(query string, maxResults int) ([]api.SearchResult, error) {
|
||||||
|
searchReq := api.SearchRequest{
|
||||||
|
Query: query,
|
||||||
|
MaxResults: maxResults,
|
||||||
|
}
|
||||||
|
|
||||||
|
client := api.NewClient(&url.URL{Scheme: "https", Host: "ollama.com"}, http.DefaultClient)
|
||||||
|
|
||||||
|
searchResp, err := client.WebSearch(context.Background(), &searchReq)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return searchResp.Results, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) FetchHandler(c *gin.Context) {
|
||||||
|
var req api.FetchRequest
|
||||||
|
if err := c.ShouldBindJSON(&req); errors.Is(err, io.EOF) {
|
||||||
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "missing request body"})
|
||||||
|
return
|
||||||
|
} else if err != nil {
|
||||||
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate required fields
|
||||||
|
if req.URL == "" {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "url is required"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call the real web fetch API
|
||||||
|
content, title, err := s.callWebFetchAPI(req.URL)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
resp := api.FetchResponse{
|
||||||
|
Content: content,
|
||||||
|
Title: title,
|
||||||
|
URL: req.URL,
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, resp)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) callWebFetchAPI(targetURL string) (string, string, error) {
|
||||||
|
// Create request to ollama.com web fetch API
|
||||||
|
fetchReq := api.FetchRequest{
|
||||||
|
URL: targetURL,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create client to call ollama.com
|
||||||
|
client := api.NewClient(&url.URL{Scheme: "https", Host: "ollama.com"}, http.DefaultClient)
|
||||||
|
|
||||||
|
// Call the web fetch API
|
||||||
|
fetchResp, err := client.Fetch(context.Background(), &fetchReq)
|
||||||
|
if err != nil {
|
||||||
|
return "", "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return fetchResp.Content, fetchResp.Title, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Server) HeadBlobHandler(c *gin.Context) {
|
func (s *Server) HeadBlobHandler(c *gin.Context) {
|
||||||
path, err := GetBlobsPath(c.Param("digest"))
|
path, err := GetBlobsPath(c.Param("digest"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -1447,6 +1549,8 @@ func (s *Server) GenerateRoutes(rc *ollama.Registry) (http.Handler, error) {
|
|||||||
r.POST("/api/chat", s.ChatHandler)
|
r.POST("/api/chat", s.ChatHandler)
|
||||||
r.POST("/api/embed", s.EmbedHandler)
|
r.POST("/api/embed", s.EmbedHandler)
|
||||||
r.POST("/api/embeddings", s.EmbeddingsHandler)
|
r.POST("/api/embeddings", s.EmbeddingsHandler)
|
||||||
|
r.POST("/api/web_search", s.WebSearchHandler)
|
||||||
|
r.POST("/api/web_fetch", s.FetchHandler)
|
||||||
|
|
||||||
// Inference (OpenAI compatibility)
|
// Inference (OpenAI compatibility)
|
||||||
r.POST("/v1/chat/completions", openai.ChatMiddleware(), s.ChatHandler)
|
r.POST("/v1/chat/completions", openai.ChatMiddleware(), s.ChatHandler)
|
||||||
|
|||||||
Reference in New Issue
Block a user