-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
mike dupont
committed
Feb 20, 2024
1 parent
b4f1922
commit 538e895
Showing
9 changed files
with
553 additions
and
0 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,208 @@ | ||
(* | ||
identifiers as units, terminals. | ||
simple sets of identifiers. | ||
objects, types and properties as non terminals. | ||
*) | ||
|
||
Inductive unit : Set := | ||
| None1 : unit | ||
| One : unit -> unit | ||
| Two : unit * unit -> unit. | ||
|
||
Definition two (x:unit):unit := Two (x, x). | ||
|
||
Definition four (x:unit):unit := Two (two x, two x). | ||
|
||
Definition eight (x:unit):unit := Two (four x, four x). | ||
|
||
Definition n16 (x:unit):unit := Two (eight x, eight x). | ||
Definition n32 (x:unit):unit := Two (n16 x, n16 x). | ||
Definition n64 (x:unit):unit := Two (n32 x, n32 x). | ||
Definition n128 (x:unit):unit := Two (n64 x, n64 x). | ||
Definition n256 (x:unit):unit := Two (n128 x, n128 x). | ||
Definition n512 (x:unit):unit := Two (n256 x, n256 x). | ||
|
||
(* | ||
now we have allocated units | ||
of different sizes recursivly, | ||
we can also define it inductivly | ||
*) | ||
|
||
Fixpoint n_items (x:unit)(y:unit):unit := | ||
match x with | ||
| None1 => None1 | ||
| One a => One (n_items a y) | ||
| Two (a,b) => Two ((n_items a y) , (n_items b y)) | ||
end. | ||
|
||
Print option. | ||
(* | ||
:= n_items (sub x) y | ||
|
||
structure become meaning | ||
meaning becomee structure. | ||
|
||
|
||
we can now use these object to encode bytes or any other object. | ||
bytes are bit patterns that allow | ||
construction of more complex things like emojis or unicode. | ||
we can imagine a regex grammar for them. | ||
we can construct grammars. | ||
|
||
terminals. None, One. | ||
non terminals (Two) | ||
|
||
Thus the structure becomes meaning. | ||
we have encoded non terminals into two, the total2 pair. | ||
|
||
we can use the LLM to expand each pair | ||
and explain its reasoning. | ||
|
||
we can limit the language to specific terms and generate a grammar for it to fill out. | ||
|
||
we can serialize the questions into strings. | ||
|
||
generate coq code whose goal is to generate this text using a large language model. | ||
|
||
Create COQ code that elicits the following text through the use of a sizable language model: | ||
*) | ||
|
||
(* Define a function that takes a large language model as an argument and generates text as output *) | ||
Definition type_of_size (u:unit) :unit := None1. | ||
|
||
Definition Char : Type := option (nat). | ||
|
||
(* Definition char : Type := type_of_size (n256 None). *) | ||
(* has the error *) | ||
(* The term "type_of_size (n256 None)" *) | ||
(* has type "unit" while it is expected to have type *) | ||
(* "Type". *) | ||
(* lets make a dummy type for this. *) | ||
Definition predict_next_char (x:unit):= None1. | ||
|
||
Definition text_length (a:unit) := None1. | ||
Definition bind (a:unit)(b:unit) : option unit := Some None1. | ||
Definition distribution_Probability := None1. | ||
Definition distribution_Value := None1. | ||
Definition raise (x:unit) := None1. | ||
Definition string := None1. | ||
|
||
Inductive amsg := | ||
| Msg_unexpected_eot. | ||
(* "Unexpected end of text during generation." *) | ||
Definition msg_to_unit (m:amsg) := None1. | ||
Definition string_sub (a:unit)(b:nat)(c:unit) := None1. | ||
|
||
Definition sample_next generated_text : unit := | ||
let distribution := predict_next_char (string_sub generated_text 0 (text_length generated_text)) in | ||
match bind distribution_Probability distribution_Value with | ||
| Some c => c | ||
| None => (raise (msg_to_unit (Msg_unexpected_eot))) | ||
end. | ||
Definition append_string (a:unit) (s:unit) := None1. | ||
Definition String_make (a:nat) (s:unit) := None1. | ||
|
||
Definition fun1 s current_char := | ||
String_make 1 ( | ||
append_string current_char s). | ||
|
||
(* ```coq *) | ||
(* Define type for tokens *) | ||
Inductive token := | ||
| Char (c : nat) (* Represent characters as natural numbers *) | ||
| Word (str : string2). | ||
|
||
(* Define type for a sequence of tokens *) | ||
Definition sequence := list token. | ||
|
||
(* Simulate statistical knowledge with a simple frequency map *) | ||
Definition freq_map := nat (token * nat). | ||
|
||
(* Function to update frequency map based on a sequence *) | ||
Definition update_freq (seq : sequence) (freq : freq_map) := | ||
foldl (fun acc t => update (snd t) 1 (fst t) acc) freq seq. | ||
|
||
(* Naive next token prediction based on most frequent char/word in current context *) | ||
Definition predict_next_naive (context : sequence) (freq : freq_map) := | ||
match context with | ||
| [] => None | ||
| [t] => match t with | ||
| Char _ => None (* Cannot predict next char without context *) | ||
| Word w => List.hd (List.sort compare (map (fun (t, _) => t) (filter (fun (_, c) => snd t = w) freq))) (* Pick most frequent word following w *) | ||
| _ => None | ||
end | ||
end. | ||
|
||
(* Example usage *) | ||
Definition example_seq := [Word "the"]; | ||
Definition example_freq := update_freq example_seq []. | ||
|
||
Example : predict_next_naive example_seq example_freq = Some (Word "quick"). (* This might not be a good prediction depending on the actual corpus used to create freq_map *) | ||
``` | ||
|
||
(* This example defines basic types for tokens, sequences, and a frequency map. It then implements two functions: *) | ||
|
||
(* * `update_freq`: Updates the frequency map based on a given sequence. *) | ||
(* * `predict_next_naive`: A simple prediction function based on the most frequent char/word in the provided context. *) | ||
|
||
(* This is a very basic example and only serves as a starting point. Remember that: *) | ||
|
||
(* * Coq is not optimized for machine learning tasks. *) | ||
(* * This doesn't represent a fully functional language model and lacks crucial elements like training, smoothing, and dealing with unseen tokens. *) | ||
|
||
(* For full-fledged language models, consider dedicated libraries or frameworks designed for machine learning within Coq. *) | ||
|
||
Definition generate_next : unit := | ||
let current_char := sample_next in | ||
let generated_text := ref "" in | ||
let generate_character current_char : unit := | ||
modify generated_text (fun s => current_char ^ s) | ||
in | ||
let cc := generate_character current_char in | ||
Printf.printf "[%c]" cc. | ||
|
||
|
||
Definition generate_text (lm : lang_model) : string := | ||
let generated_text : string := "" in | ||
(* Function to sample next character from the language model *) | ||
|
||
(* Function to generate the next character and update the generated text *) | ||
|
||
(* Generate the text *) | ||
generate_next (); | ||
while not (String.is_empty !generated_text) do | ||
generate_next (); | ||
done; | ||
!generated_text. | ||
|
||
(* Instantiate a large language model and call the generate_text function *) | ||
Let lm : lang_model := (create_large_language_model "path/to/your/pretrained/model"); | ||
Printinf "Generated text: %A" (generate_text lm); | ||
``` | ||
|
||
(* In this code, we define a `generate_text` function that takes a large language model as an argument and generates text based on the given model. This function uses `sample_next` to generate the next character according to the language model's predictions. The `generate_text` function then generates each subsequent character by updating the generated text accordingly and printing it out. Finally, once the generated text is empty (i.e., the end of the text has been reached), the function returns the complete generated text. We also create a large language model instance called 'lm' using `create_large_language_model` and then call the `generate_text` function to generate the text. *) | ||
|
||
Inductive grmr_nt (Γ : Type) : Type := | ||
| Var : symbol -> grmr_nt Γ | ||
| App : grmr_nt Γ -> grmr_nt Γ -> grmr_nt Γ | ||
| Abs : (Γ -> grmr_nt Γ) -> grmr_nt Γ | ||
| Fail : info -> grmr_nt Γ (* Stores parsing failure information *) | ||
|
||
Inductive info : Type := | ||
| FailInfo : symbol -> list grmr_nt Γ -> info (* Specific non-terminal, context *) | ||
| TimeoutInfo : info | ||
|
||
(* Functors for grammar monad *) | ||
Definition functor (F : {f : Γ -> Γ} {g : grmr_nt Γ -> grmr_nt (F Γ)}) := ... | ||
|
||
(* Example functors *) | ||
Definition simplify : functor := ... | ||
Definition type_check : functor := ... | ||
|
||
(* Grammar refinement loop *) | ||
Fixpoint refine_grammar (G : grmr_nt Γ) : grmr_nt Γ := | ||
let analyzed_grammar := ... (* Apply analysis functors *) | ||
let expected_grammar := ... (* Obtain expected grammar *) | ||
let new_grammar := ... (* Train and generate with language model *) | ||
(* Evaluate and update grammar based on loss function and feedback *) | ||
. |
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 @@ | ||
mdupont@mdupont-G470.22257:1702745021 |
Submodule coq-ai
added at
a4342d
Submodule coq-llm-rl
added at
3ab63d
Submodule coq_llm_interaction
added at
403056
Oops, something went wrong.