Skip to content

Instantly share code, notes, and snippets.

@joeke80215
Last active February 5, 2024 06:46
Show Gist options
  • Save joeke80215/554c02dcb4bcc40bb11e8a983db11451 to your computer and use it in GitHub Desktop.
Save joeke80215/554c02dcb4bcc40bb11e8a983db11451 to your computer and use it in GitHub Desktop.
Using a reverse proxy for the ChatGPT API to interface with Perplexity.
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
)
type Message struct {
Role string `json:"role"`
Content string `json:"content"`
}
type Request struct {
Temperature float64 `json:"temperature"`
Stream bool `json:"stream"`
Model string `json:"model"`
Messages []Message `json:"messages"`
MaxTokens int `json:"max_tokens"`
FrequencyPenalty *float64 `json:"frequency_penalty,omitempty"`
PresencePenalty *float64 `json:"presence_penalty,omitempty"`
}
func main() {
// This is the OpenAI API base URL
target := "https://api.perplexity.ai/chat/completions"
// Handle all requests to your server using the proxy
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// get body
body, err := io.ReadAll(r.Body)
if err != nil {
log.Panic(err)
}
var request Request
if err := json.Unmarshal(body, &request); err != nil {
log.Panic(err)
}
request.Model = "codellama-70b-instruct"
request.FrequencyPenalty = nil
reqB, err := json.Marshal(request)
if err != nil {
log.Panic(err)
}
req, err := http.NewRequest("POST", target, bytes.NewBuffer(reqB))
if err != nil {
fmt.Println(err)
return
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", r.Header.Get("Authorization"))
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer func() { _ = resp.Body.Close() }()
w.Header().Set("Content-Type", "text/event-stream")
_, _ = io.Copy(w, resp.Body)
})
// Start the server
log.Fatal(http.ListenAndServe(":8080", nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment