-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Render OpenAI request/response as HTML (#265)
This PR adds support for rendering of OpenAI requests/responses as HTML (fix #259). This functionality was already supported for Anthropic. I added an LLM sub-command to support rendering the data as HTML. This makes it reusable and not closely tied to the GetLLMLogs RPC. Fix HTML rendering of our prompts to properly handle the XML tags in our prompt. We need to escape them so they aren't treated as raw HTML tags which don't render. Remove the obsolete logs subcommand. Fix #259
- Loading branch information
Showing
8 changed files
with
382 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/jlewi/monogo/helpers" | ||
|
||
"github.com/jlewi/foyle/app/api" | ||
"github.com/jlewi/foyle/app/pkg/analyze" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// NewLLMsCmd returns a command to work with llms | ||
func NewLLMsCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "llms", | ||
} | ||
|
||
cmd.AddCommand(NewLLMsRenderCmd()) | ||
|
||
return cmd | ||
} | ||
|
||
func NewLLMsRenderCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "render", | ||
} | ||
|
||
cmd.AddCommand(NewLLMsRenderRequestCmd()) | ||
cmd.AddCommand(NewLLMsRenderResponseCmd()) | ||
|
||
return cmd | ||
} | ||
|
||
func NewLLMsRenderRequestCmd() *cobra.Command { | ||
var provider string | ||
var inputFile string | ||
var outputFile string | ||
cmd := &cobra.Command{ | ||
Use: "request", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
err := func() error { | ||
p := api.ModelProvider(provider) | ||
|
||
data, err := os.ReadFile(inputFile) | ||
if err != nil { | ||
return errors.Wrapf(err, "Failed to read file %s", inputFile) | ||
} | ||
|
||
htmlData, err := analyze.RenderRequestHTML(string(data), p) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if outputFile != "" { | ||
if err := os.WriteFile(outputFile, []byte(htmlData), 0644); err != nil { | ||
return errors.Wrapf(err, "Failed to write file %s", outputFile) | ||
} | ||
} else { | ||
fmt.Fprint(os.Stdout, htmlData) | ||
} | ||
|
||
return nil | ||
}() | ||
|
||
if err != nil { | ||
fmt.Printf("Error rendering request;\n %+v\n", err) | ||
os.Exit(1) | ||
} | ||
}, | ||
} | ||
|
||
cmd.Flags().StringVarP(&provider, "provider", "p", string(api.ModelProviderOpenAI), "The model provider for the request.") | ||
cmd.Flags().StringVarP(&inputFile, "input", "i", "", "The file containing the JSON representation of the request.") | ||
cmd.Flags().StringVarP(&inputFile, "output", "o", "", "The file to write the output to. If blank output to stdout.") | ||
helpers.IgnoreError(cmd.MarkFlagRequired("input")) | ||
|
||
return cmd | ||
} | ||
|
||
func NewLLMsRenderResponseCmd() *cobra.Command { | ||
var provider string | ||
var inputFile string | ||
var outputFile string | ||
cmd := &cobra.Command{ | ||
Use: "response", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
err := func() error { | ||
p := api.ModelProvider(provider) | ||
|
||
data, err := os.ReadFile(inputFile) | ||
if err != nil { | ||
return errors.Wrapf(err, "Failed to read file %s", inputFile) | ||
} | ||
|
||
htmlData, err := analyze.RenderResponseHTML(string(data), p) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if outputFile != "" { | ||
if err := os.WriteFile(outputFile, []byte(htmlData), 0644); err != nil { | ||
return errors.Wrapf(err, "Failed to write file %s", outputFile) | ||
} | ||
} else { | ||
fmt.Fprint(os.Stdout, htmlData) | ||
} | ||
|
||
return nil | ||
}() | ||
|
||
if err != nil { | ||
fmt.Printf("Error rendering response;\n %+v\n", err) | ||
os.Exit(1) | ||
} | ||
}, | ||
} | ||
|
||
cmd.Flags().StringVarP(&provider, "provider", "p", string(api.ModelProviderOpenAI), "The model provider for the request.") | ||
cmd.Flags().StringVarP(&inputFile, "input", "i", "", "The file containing the JSON representation of the request.") | ||
cmd.Flags().StringVarP(&inputFile, "output", "o", "", "The file to write the output to. If blank output to stdout.") | ||
helpers.IgnoreError(cmd.MarkFlagRequired("input")) | ||
|
||
return cmd | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.